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?
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(urls[0])
.build();
Response responses = null;
try {
responses = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
String jsonData = responses.body().string();
JSONObject Jobject = new JSONObject(jsonData);
JSONArray Jarray = Jobject.getJSONArray("employees");
for (int i = 0; i < Jarray.length(); i++) {
JSONObject object = Jarray.getJSONObject(i);
}
}
Example add to your columns:
JCol employees = new employees();
colums.Setid(object.getInt("firstName"));
columnlist.add(lastName);