-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java.lang.IllegalStateException: closed when trying to access response in onResponse(Response response) #1240
Comments
Are you reading the response body 2x? You can only call |
That was probably the case. |
You can only call string() once. true |
You can call string() once. But I don't know why |
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. On Tue, Jul 19, 2016 at 5:46 PM fattolium <
You can call string() once. But I don't know why — You are receiving this because you are subscribed to this thread. Reply to this email directly, |
once requst once download |
response.body.string should be defined as a variable.but in fact ,i also don't why this. |
Pffft. At least give people the option to do that if they want! Exceptions don't help much. |
@artem-zinnatullin thanks very much! |
Beware if you have response.body().string() in your watch list that will cause your code to fail. Since watch list will run automatically. |
Thanks a lot....solved a huge problem for me 👍 |
if you have used response.body().string() once then you can new one same response and return in case that you will use "response.body().string()" again. It won't throw any exception if you do this. |
At least it would be nice to add explanation to javadocs about this. |
Which Javadocs did you read? That’s where we should add such a note. |
It's documented on ResponseBody.
…On Wed, Sep 13, 2017 at 11:04 AM Włodzimierz Rożkow < ***@***.***> wrote:
At least it would be nice to add explanation to javadocs about this.
I just broke my production because of twice call to string()
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1240 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAEEEZm2BB9oiE_q1GHhuSJ8E3vD_JiRks5sh-72gaJpZM4DMQVK>
.
|
I've read javadocs for
Maybe my version is a bit outdated? 2.7.5 |
Try the following:
it works for me |
@KaNcHeR It looks like a dirty hack, but it works :D Thanks a lot |
Don't try to use Reason: |
…g() twice Summary: Fixing from call response.body.string() twice in DevServerHelper.java. square/okhttp#1240 (comment) I'm getting error like this ``` 05-01 21:16:47.080 22836-23064/com.my.company.bundle E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher Process: com.my.company.bundle, PID: 22836 java.lang.IllegalStateException: closed at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:398) at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:392) at okhttp3.internal.c.a(Util.java:449) at okhttp3.v.string(ResponseBody.java:174) at com.facebook.react.devsupport.f$8.onResponse(DevServerHelper.java:487) at com.newrelic.agent.android.instrumentation.okhttp3.CallbackExtension.onResponse(CallbackExtension.java:41) at okhttp3.s$a.c(RealCall.java:153) at okhttp3.internal.b.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) ``` <!-- Required: Write your motivation here. If this PR fixes an issue, type "Fixes #issueNumber" to automatically close the issue when the PR is merged. --> - In my case, My device is using a Proxy, and tried to connect the packager. When it failed from connecting packager through websocket, It crash by this line of code. <!-- Required: Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos! --> <!-- Does this PR require a documentation change? Create a PR at https://github.com/facebook/react-native-website and add a link to it here. --> <!-- Required. Help reviewers and the release process by writing your own release notes. See below for an example. --> [ANDROID] [BUGFIX] [DevServerHelper] - fixing from calling body.string() twice. <!-- **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [ {Component} ] [ INTERNAL ] [ ENHANCEMENT ] [ {Filename} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> Closes #19088 Differential Revision: D7853822 Pulled By: mdvacca fbshipit-source-id: c11a73ce2fa6d40b0a7bd8bc0fca7b07c6bc27ed
Check own okhttp interceptor's, throuble may be inside. |
How can I do that? It gives an error if it is in a different thread ? |
…g() twice Summary: Fixing from call response.body.string() twice in DevServerHelper.java. square/okhttp#1240 (comment) I'm getting error like this ``` 05-01 21:16:47.080 22836-23064/com.my.company.bundle E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher Process: com.my.company.bundle, PID: 22836 java.lang.IllegalStateException: closed at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:398) at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:392) at okhttp3.internal.c.a(Util.java:449) at okhttp3.v.string(ResponseBody.java:174) at com.facebook.react.devsupport.f$8.onResponse(DevServerHelper.java:487) at com.newrelic.agent.android.instrumentation.okhttp3.CallbackExtension.onResponse(CallbackExtension.java:41) at okhttp3.s$a.c(RealCall.java:153) at okhttp3.internal.b.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) ``` <!-- Required: Write your motivation here. If this PR fixes an issue, type "Fixes #issueNumber" to automatically close the issue when the PR is merged. --> - In my case, My device is using a Proxy, and tried to connect the packager. When it failed from connecting packager through websocket, It crash by this line of code. <!-- Required: Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos! --> <!-- Does this PR require a documentation change? Create a PR at https://github.com/facebook/react-native-website and add a link to it here. --> <!-- Required. Help reviewers and the release process by writing your own release notes. See below for an example. --> [ANDROID] [BUGFIX] [DevServerHelper] - fixing from calling body.string() twice. <!-- **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [ {Component} ] [ INTERNAL ] [ ENHANCEMENT ] [ {Filename} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> Closes facebook#19088 Differential Revision: D7853822 Pulled By: mdvacca fbshipit-source-id: c11a73ce2fa6d40b0a7bd8bc0fca7b07c6bc27ed
Response body is not stored in memory. .string() reads as stream from network. That is it. |
Try storing response in string variable separately like this, String res = response.body().string(); Then use variable res. |
how come. it is not working |
For me the problem was the wrapper function that returned
|
I tried something like this Response response = chain.proceed(newRequest); responseTmp.body().string(); and it works |
That makes two HTTP requests. You don't want to do that. |
Adding this bit as it may help someone else. I was puzzled to encounter the very same exception while calling The problem, I realized later, was due to the execution context of my request: using a try with resources block with the response causes the same problem when the response body is read outside of the try block:
If |
just call response.close(); example : @OverRide
|
I encountered the same issue, receiving an Exception when reading the response, e.g. |
why each api gets called twice ? |
I get
when trying to access the response body using:
inside onResponse(Response response) callback.
The text was updated successfully, but these errors were encountered: