extracting values from JSON response
from rest service I receive below json respoonse
{"code":1,"message":"success","status":"success","users":[{"email":"qqq@aa.aa","name":"qq"},{"email":"dd@dd.dd","name":"dd"},{"email":"cc@vv.vv","name":"cc"},{"email":"qq@qq.qq","name":"qq"},{"email":"qq@qq.qq","name":"qq"}]}
When I tried to convert this into JSONObject it throws an error,
this is how I tried to extract the JSON response,
jsonObject = new JSONObject(response.body().string());
The error is thrown from above script. Error is below,
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.ej.ziphiotest, PID: 21945
java.lang.IllegalStateException: closed
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:408)
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:402)
at okhttp3.internal.Util.bomAwareCharset(Util.java:432)
at okhttp3.ResponseBody.string(ResponseBody.java:174)
at com.example.ej.ziphiotest.requests.UserRequest$1.onResponse(UserRequest.java:85)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
EDIT:
This is request:
final Request request = new Request.Builder()
.header("key", params[0])
.url(URL)
.build();
try {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code() == 200){
try {
jsonObject = new JSONObject(response.body().string());
restResponse = new RestResponse(jsonObject.getString("status"), jsonObject.getString("code"), jsonObject.getString("message"), null, jsonObject.getJSONArray("users"));
onTaskComplete.onTaskComplete(restResponse);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Where I missed. Thank you fro your valuable time
java
|
show 7 more comments
from rest service I receive below json respoonse
{"code":1,"message":"success","status":"success","users":[{"email":"qqq@aa.aa","name":"qq"},{"email":"dd@dd.dd","name":"dd"},{"email":"cc@vv.vv","name":"cc"},{"email":"qq@qq.qq","name":"qq"},{"email":"qq@qq.qq","name":"qq"}]}
When I tried to convert this into JSONObject it throws an error,
this is how I tried to extract the JSON response,
jsonObject = new JSONObject(response.body().string());
The error is thrown from above script. Error is below,
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.ej.ziphiotest, PID: 21945
java.lang.IllegalStateException: closed
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:408)
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:402)
at okhttp3.internal.Util.bomAwareCharset(Util.java:432)
at okhttp3.ResponseBody.string(ResponseBody.java:174)
at com.example.ej.ziphiotest.requests.UserRequest$1.onResponse(UserRequest.java:85)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
EDIT:
This is request:
final Request request = new Request.Builder()
.header("key", params[0])
.url(URL)
.build();
try {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code() == 200){
try {
jsonObject = new JSONObject(response.body().string());
restResponse = new RestResponse(jsonObject.getString("status"), jsonObject.getString("code"), jsonObject.getString("message"), null, jsonObject.getJSONArray("users"));
onTaskComplete.onTaskComplete(restResponse);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Where I missed. Thank you fro your valuable time
java
replace this "jsonObject = new JSONObject(response.body().string());" with "jsonObject = new JSONObject(response);"
– pratik vekariya
Nov 14 '18 at 4:52
to me it doesn't seem like the json error. Can you post your api calling code
– Vivek Mishra
Nov 14 '18 at 4:53
1
Are you callingresponse.body().string();twice, I mean once for logging it and other for converting to JsonObject and processing it?
– Firoz Memon
Nov 14 '18 at 5:04
1
@EJChathuranga Do not call response body more than once, since it is read as a stream and not stored in memory Reference: github.com/square/okhttp/issues/1240
– Firoz Memon
Nov 14 '18 at 5:08
1
@EJChathuranga Try storing it in a variable and then using the variable, not theresponse.body().string()code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.
– Firoz Memon
Nov 14 '18 at 5:12
|
show 7 more comments
from rest service I receive below json respoonse
{"code":1,"message":"success","status":"success","users":[{"email":"qqq@aa.aa","name":"qq"},{"email":"dd@dd.dd","name":"dd"},{"email":"cc@vv.vv","name":"cc"},{"email":"qq@qq.qq","name":"qq"},{"email":"qq@qq.qq","name":"qq"}]}
When I tried to convert this into JSONObject it throws an error,
this is how I tried to extract the JSON response,
jsonObject = new JSONObject(response.body().string());
The error is thrown from above script. Error is below,
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.ej.ziphiotest, PID: 21945
java.lang.IllegalStateException: closed
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:408)
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:402)
at okhttp3.internal.Util.bomAwareCharset(Util.java:432)
at okhttp3.ResponseBody.string(ResponseBody.java:174)
at com.example.ej.ziphiotest.requests.UserRequest$1.onResponse(UserRequest.java:85)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
EDIT:
This is request:
final Request request = new Request.Builder()
.header("key", params[0])
.url(URL)
.build();
try {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code() == 200){
try {
jsonObject = new JSONObject(response.body().string());
restResponse = new RestResponse(jsonObject.getString("status"), jsonObject.getString("code"), jsonObject.getString("message"), null, jsonObject.getJSONArray("users"));
onTaskComplete.onTaskComplete(restResponse);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Where I missed. Thank you fro your valuable time
java
from rest service I receive below json respoonse
{"code":1,"message":"success","status":"success","users":[{"email":"qqq@aa.aa","name":"qq"},{"email":"dd@dd.dd","name":"dd"},{"email":"cc@vv.vv","name":"cc"},{"email":"qq@qq.qq","name":"qq"},{"email":"qq@qq.qq","name":"qq"}]}
When I tried to convert this into JSONObject it throws an error,
this is how I tried to extract the JSON response,
jsonObject = new JSONObject(response.body().string());
The error is thrown from above script. Error is below,
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.ej.ziphiotest, PID: 21945
java.lang.IllegalStateException: closed
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:408)
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:402)
at okhttp3.internal.Util.bomAwareCharset(Util.java:432)
at okhttp3.ResponseBody.string(ResponseBody.java:174)
at com.example.ej.ziphiotest.requests.UserRequest$1.onResponse(UserRequest.java:85)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
EDIT:
This is request:
final Request request = new Request.Builder()
.header("key", params[0])
.url(URL)
.build();
try {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code() == 200){
try {
jsonObject = new JSONObject(response.body().string());
restResponse = new RestResponse(jsonObject.getString("status"), jsonObject.getString("code"), jsonObject.getString("message"), null, jsonObject.getJSONArray("users"));
onTaskComplete.onTaskComplete(restResponse);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Where I missed. Thank you fro your valuable time
java
java
edited Nov 14 '18 at 4:59
E J Chathuranga
asked Nov 14 '18 at 4:50
E J ChathurangaE J Chathuranga
317413
317413
replace this "jsonObject = new JSONObject(response.body().string());" with "jsonObject = new JSONObject(response);"
– pratik vekariya
Nov 14 '18 at 4:52
to me it doesn't seem like the json error. Can you post your api calling code
– Vivek Mishra
Nov 14 '18 at 4:53
1
Are you callingresponse.body().string();twice, I mean once for logging it and other for converting to JsonObject and processing it?
– Firoz Memon
Nov 14 '18 at 5:04
1
@EJChathuranga Do not call response body more than once, since it is read as a stream and not stored in memory Reference: github.com/square/okhttp/issues/1240
– Firoz Memon
Nov 14 '18 at 5:08
1
@EJChathuranga Try storing it in a variable and then using the variable, not theresponse.body().string()code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.
– Firoz Memon
Nov 14 '18 at 5:12
|
show 7 more comments
replace this "jsonObject = new JSONObject(response.body().string());" with "jsonObject = new JSONObject(response);"
– pratik vekariya
Nov 14 '18 at 4:52
to me it doesn't seem like the json error. Can you post your api calling code
– Vivek Mishra
Nov 14 '18 at 4:53
1
Are you callingresponse.body().string();twice, I mean once for logging it and other for converting to JsonObject and processing it?
– Firoz Memon
Nov 14 '18 at 5:04
1
@EJChathuranga Do not call response body more than once, since it is read as a stream and not stored in memory Reference: github.com/square/okhttp/issues/1240
– Firoz Memon
Nov 14 '18 at 5:08
1
@EJChathuranga Try storing it in a variable and then using the variable, not theresponse.body().string()code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.
– Firoz Memon
Nov 14 '18 at 5:12
replace this "jsonObject = new JSONObject(response.body().string());" with "jsonObject = new JSONObject(response);"
– pratik vekariya
Nov 14 '18 at 4:52
replace this "jsonObject = new JSONObject(response.body().string());" with "jsonObject = new JSONObject(response);"
– pratik vekariya
Nov 14 '18 at 4:52
to me it doesn't seem like the json error. Can you post your api calling code
– Vivek Mishra
Nov 14 '18 at 4:53
to me it doesn't seem like the json error. Can you post your api calling code
– Vivek Mishra
Nov 14 '18 at 4:53
1
1
Are you calling
response.body().string(); twice, I mean once for logging it and other for converting to JsonObject and processing it?– Firoz Memon
Nov 14 '18 at 5:04
Are you calling
response.body().string(); twice, I mean once for logging it and other for converting to JsonObject and processing it?– Firoz Memon
Nov 14 '18 at 5:04
1
1
@EJChathuranga Do not call response body more than once, since it is read as a stream and not stored in memory Reference: github.com/square/okhttp/issues/1240
– Firoz Memon
Nov 14 '18 at 5:08
@EJChathuranga Do not call response body more than once, since it is read as a stream and not stored in memory Reference: github.com/square/okhttp/issues/1240
– Firoz Memon
Nov 14 '18 at 5:08
1
1
@EJChathuranga Try storing it in a variable and then using the variable, not the
response.body().string() code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.– Firoz Memon
Nov 14 '18 at 5:12
@EJChathuranga Try storing it in a variable and then using the variable, not the
response.body().string() code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.– Firoz Memon
Nov 14 '18 at 5:12
|
show 7 more comments
2 Answers
2
active
oldest
votes
The issue was I was using response.body().string() twice within very short amount of time. AS @Firoz Memon saying in the comments.
Try storing it in a variable and then using the variable, not the response.body().string() code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.
copied from @Firoz Memon commenting from above.
Original Reference: https://github.com/square/okhttp/issues/1240
add a comment |
First parse the Object then Object array like this way:
Don't try to use response.body.string() directly. Just using like below:
ResponseBody responseBody = response.body();
String content = responseBody.string();
// Do something with "content" variable
Reason:
The the string of body is stored in memory to variable content, so we don't need to care of whether the state is closed.
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray =jsonObject.getJSONArray("users");
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293364%2fextracting-values-from-json-response%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue was I was using response.body().string() twice within very short amount of time. AS @Firoz Memon saying in the comments.
Try storing it in a variable and then using the variable, not the response.body().string() code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.
copied from @Firoz Memon commenting from above.
Original Reference: https://github.com/square/okhttp/issues/1240
add a comment |
The issue was I was using response.body().string() twice within very short amount of time. AS @Firoz Memon saying in the comments.
Try storing it in a variable and then using the variable, not the response.body().string() code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.
copied from @Firoz Memon commenting from above.
Original Reference: https://github.com/square/okhttp/issues/1240
add a comment |
The issue was I was using response.body().string() twice within very short amount of time. AS @Firoz Memon saying in the comments.
Try storing it in a variable and then using the variable, not the response.body().string() code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.
copied from @Firoz Memon commenting from above.
Original Reference: https://github.com/square/okhttp/issues/1240
The issue was I was using response.body().string() twice within very short amount of time. AS @Firoz Memon saying in the comments.
Try storing it in a variable and then using the variable, not the response.body().string() code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.
copied from @Firoz Memon commenting from above.
Original Reference: https://github.com/square/okhttp/issues/1240
edited Nov 14 '18 at 5:46
Firoz Memon
1,69421021
1,69421021
answered Nov 14 '18 at 5:18
E J ChathurangaE J Chathuranga
317413
317413
add a comment |
add a comment |
First parse the Object then Object array like this way:
Don't try to use response.body.string() directly. Just using like below:
ResponseBody responseBody = response.body();
String content = responseBody.string();
// Do something with "content" variable
Reason:
The the string of body is stored in memory to variable content, so we don't need to care of whether the state is closed.
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray =jsonObject.getJSONArray("users");
add a comment |
First parse the Object then Object array like this way:
Don't try to use response.body.string() directly. Just using like below:
ResponseBody responseBody = response.body();
String content = responseBody.string();
// Do something with "content" variable
Reason:
The the string of body is stored in memory to variable content, so we don't need to care of whether the state is closed.
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray =jsonObject.getJSONArray("users");
add a comment |
First parse the Object then Object array like this way:
Don't try to use response.body.string() directly. Just using like below:
ResponseBody responseBody = response.body();
String content = responseBody.string();
// Do something with "content" variable
Reason:
The the string of body is stored in memory to variable content, so we don't need to care of whether the state is closed.
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray =jsonObject.getJSONArray("users");
First parse the Object then Object array like this way:
Don't try to use response.body.string() directly. Just using like below:
ResponseBody responseBody = response.body();
String content = responseBody.string();
// Do something with "content" variable
Reason:
The the string of body is stored in memory to variable content, so we don't need to care of whether the state is closed.
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray =jsonObject.getJSONArray("users");
edited Nov 14 '18 at 5:21
answered Nov 14 '18 at 5:11
Sultan MahmudSultan Mahmud
23017
23017
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293364%2fextracting-values-from-json-response%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
replace this "jsonObject = new JSONObject(response.body().string());" with "jsonObject = new JSONObject(response);"
– pratik vekariya
Nov 14 '18 at 4:52
to me it doesn't seem like the json error. Can you post your api calling code
– Vivek Mishra
Nov 14 '18 at 4:53
1
Are you calling
response.body().string();twice, I mean once for logging it and other for converting to JsonObject and processing it?– Firoz Memon
Nov 14 '18 at 5:04
1
@EJChathuranga Do not call response body more than once, since it is read as a stream and not stored in memory Reference: github.com/square/okhttp/issues/1240
– Firoz Memon
Nov 14 '18 at 5:08
1
@EJChathuranga Try storing it in a variable and then using the variable, not the
response.body().string()code. Because response body can be huge so OkHttp doesn’t store it in memory, it reads it as a stream from network when you need it. When you read body as a string() OkHttp downloads response body and returns it to you without keeping reference to the string, it can’t be downloaded twice without new request.– Firoz Memon
Nov 14 '18 at 5:12