I am trying to download some JSON from the google book API.
The URL and API-Key I’m using seems to work becuase i can fetch it manually with a browser. I call this class and use this function passing it a usable URL. I’ve tried post and get(You can see where I’ve commited out the other.
While Im debugging I can watch it start the download process or it seems to but it always comes returns null. It seems like it should be working. any ideas of what might be happening? A permission or something.(Already added 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;
}
}
I guess this is the logcat (Im somewhat new to this, some of it on the top might because I switched my phone from wifi to reg. Phone Data)
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
Ok, after some informative input and research I’ve found a connection method that connects to the google book api and retrieves the JSON. The connection does use the TrustEveryOne class but it does retrieve the JSON. I’ll have to work on certificate auth later, app isnt distributed yet.