I am having some trouble getting a HTTP GET to work. I have tried with the OKHttp lib and the default android HTTP client. The problem is that I am getting this in the response body when I use the default http client
org.apache.http.conn.BasicManagedEntity@424ab550
and this when I use the okhttp lib
com.squareup.okhttp.Call$RealResponseBody@424a6c58
The response body is suppose to contain a JSON object. If I paste the URL in a browser window the JOSN is returned correctly. I have also checked the server logs and the response is getting sent correctly form the server. I am calling the HTTP GET method from an async class.
The GET method using the default Android HTTP client:
public String getData(String url)
{
Log.w("Rakshak", "in the get Data data method");
Log.w("Rakshak", "URL: "+url);
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 20000000); //Timeout Limit
HttpResponse httpResponse;
try {
Log.w("Rakshak", "in the get data try");
HttpGet get = new HttpGet(url);
httpResponse = client.execute(get);
String response = httpResponse.getEntity().toString();
Log.w("Rakshak", "Get responce: "+response); // this posts "org.apache.http.conn.BasicManagedEntity@424ab550" insted of the JSON that am supposed to get
return response;
} catch(Exception e)
{
e.printStackTrace();
}
return "1";
}
The GET method using the OkHTTP lib:
public String getData(String url)
{
OkHttpClient client = new OkHttpClient();
//Log.w("Rakshak", "in the get Data data method ");
//Log.w("Rakshak", "URL: "+url);
Request request = new Request.Builder()
.url(url)
.build();
try {
Response response = client.newCall(request).execute();
response.toString();
Log.w("Rakshak", " in the service handeler"+response.body().toString()); // this posts "com.squareup.okhttp.Call$RealResponseBody@424a6c58" insted of the JSON that I supposed to get in the body.
return response.body().toString();
} catch (IOException e) {
e.printStackTrace();
return "failed";
}
}
What am I doing wrong, why cant I get the correct response from the app when it works perfectly fine when I past the same url in a browser window?
Let me know if you need to see any more of the code.
Cheers