我正在尝试从google book API下载一些JSON。
我正在使用的URL和API-Key似乎有效,因为我可以使用浏览器手动获取它。我调用这个类并使用此函数传递一个可用的URL。我已经尝试发帖并获得(你可以看到我把其他人提交到了哪里。
虽然我正在调试我可以看到它开始下载过程或似乎但它总是返回null。好像应该有效。关于可能发生的事情的任何想法?许可或其他。(已添加 INTERNET PERMISSION )
public static class JSONFunctions2 {
public static JSONObject getJSONfromURL(String url){
/*//initialize*/
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
/*DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
HttpResponse getResponse;
getResponse = client.execute(getRequest);
HttpEntity getResponseEntity = getResponse.getEntity();
if (getResponseEntity != null) {
result= EntityUtils.toString(getResponseEntity);
}*/
/*//try parse the string to a JSON object*/
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}
我想这是logcat(我对此有点新,其中一些可能是因为我将手机从wifi切换到reg。手机数据)
11-06 23:02:23.716: ERROR/InputDispatcher(159): channel '4081c258 com.buddy/com.BuddyListActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
11-06 23:02:23.716: ERROR/InputDispatcher(159): channel '4081c258 com.buddy/com.BuddyListActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
11-06 23:03:36.616: ERROR/log_tag(4116): Error in http connection javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
11-06 23:03:36.616: ERROR/log_tag(4116): Error converting result java.lang.NullPointerException
11-06 23:04:04.766: ERROR/log_tag(4116): Error parsing data org.json.JSONException: End of input at character 0 of
答案 0 :(得分:1)
我通常这样做,也可能适合你
HttpResponse response = httpClient.execute(httpPost, localContext);
serverResponseJSON = EntityUtils.toString(response.getEntity());
Log.i("Server Response", serverResponseJSON);
答案 1 :(得分:1)
我使用此代码从webservice获取响应,并且工作正常
try {
HttpPost request = new HttpPost(
"webservice url");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
JSONStringer loginuser = new JSONStringer().object().key("userid")
.value(SetGetValues.getUserid()).endObject();
StringEntity entity = new StringEntity(loginuser.toString());
request.setEntity(entity);
Log.v("data", loginuser.toString());
// Send request to WCF service
DefaultHttpClient httpClient1 = new DefaultHttpClient();
HttpResponse response = httpClient1.execute(request);
Log.v("response code", response.getStatusLine().getStatusCode()
+ "");
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int) responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
results = new JSONArray(new String(buffer));
}catch(Exception e){
// TODO: handle exception
e.printStackTrace();
}
查看以下行
Log.v("response code", response.getStatusLine().getStatusCode()
+ "");
如果它的值是200,那么你从服务器获得正确的响应,否则你没有得到服务器的响应
答案 2 :(得分:0)
好的,经过一些信息性的输入和研究后,我发现了一种连接到谷歌书api的连接方法,并检索了JSON。该连接确实使用TrustEveryOne类,但它确实检索了JSON。我将不得不在以后使用证书身份验证,app尚未分发。
public static JSONObject getJSONfromURL(String urlpass) throws ParseException, IOException{
/*//initialize*/
InputStream is = null;
String result = "";
JSONObject jArray = null;
int response = -1;
URL url = new URL(urlpass);
TrustEveryone.trustEveryone();
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
is = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
/*//try parse the string to a JSON object*/
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}