How does OkHttp get Json string?
As I observed in my code. If once the value is fetched of body from Response, its become blank.
String str = response.body().string(); // {response:[]}
String str1 = response.body().string(); // 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.
I am also faced the same issue
use this code:
// notice string() call
String resStr = response.body().string();
JSONObject json = new JSONObject(resStr);
it definitely works
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);