How does OkHttp get Json string?
Solution 1:
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);
Solution 2:
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