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 cl
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.
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;
}
I normally does this way,might work for you as well
HttpResponse response = httpClient.execute(httpPost, localContext);
serverResponseJSON = EntityUtils.toString(response.getEntity());
Log.i("Server Response", serverResponseJSON);
I use this code to get response from webservice and it works fine
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();
}
check out the following line
Log.v("response code", response.getStatusLine().getStatusCode()
+ "");
if its value is 200 then you r getting ok response from server else you are not getting response from server