Solution: It was a mistake on my side.
The right way is response.body().string() other than response.body.toString()
Im using Jetty servlet, the URL ishttp://172.16.10.126:8789/test/path/jsonpage
, every time request this URL it will return
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
It shows up when type the url into a browser, unfortunately it shows kind of memory address other than the json string when I request with Okhttp
.
TestActivity﹕ com.squareup.okhttp.internal.http.RealResponseBody@537a7f84
The Okhttp code Im using:
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
Can anyone helpe?
I am also faced the same issue
use this code:
it definitely works
I hope you managed to obtain the json data from the json string.
Well I think this will be of help
Remember to use AsyncTask or SyncAdapter(IntentService), to prevent getting a NetworkOnMainThreadException
Also import the okhttp library in your build.gradle
compile 'com.squareup.okhttp:okhttp:2.4.0'
As I observed in my code. If once the value is fetched of body from Response, its become blank.
So I believe after fetching once the value from body, it become empty.
Suggestion : Store it in String, that can be used many time.
Example add to your columns: