Skip to content
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 #22

Open
wolfchkov opened this issue Feb 18, 2020 · 5 comments
Open

java.lang.IllegalStateException: closed #22

wolfchkov opened this issue Feb 18, 2020 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@wolfchkov
Copy link

wolfchkov commented Feb 18, 2020

This error occur when read okhttp3.response.body() more then one time, because it is stream, after first read it is closing.

.....
onResponse: (call, response) => {
   let body;
   try {
       body = JSON.parse(response.body().string()); //error with parse JSON
   } catch (e) {
       body = response.body().string(); //reading body second time,  crash 
   }
.....
@wolfchkov
Copy link
Author

wolfchkov commented Feb 18, 2020

I found out that in my case raised error "android.os.NetworkOnMainThreadException" because of reading of response body in the httpOk callback, that executes in main thread.
I have no idea how to fix it.

Reading the response body may still block

@dotnetdreamer
Copy link
Owner

dotnetdreamer commented Feb 19, 2020

I think this could work then.

onResponse: (call, response) => {
   let body = response.body().string();
   try {
       body = JSON.parse(body); //error with parse JSON
   } catch (e) { //ignore }
}

or a null check for body...

@wolfchkov
Copy link
Author

wolfchkov commented Feb 19, 2020

But, as I said earlier, the second reads of response body it is not a cause, it is a consequence of threw android.os.NetworkOnMainThreadException, because of IO operation on the main thread(The callback is made after the response headers are ready, so response body reading is a IO operation).

Seems, that one way to resolve this issue it read response body in separate thread e.g. AsyncTask.

onResponse: (call, response) => {
   let body = response.body().string(); // it is Blocking operation that execute on the main thread, 
                                                                 // so Android throws NetworkOnMainThreadException
                                                              
   try {
       body = JSON.parse(body); //error with parse JSON
   } catch (e) { //ignore }
}

@dotnetdreamer
Copy link
Owner

@wolfchkov a temp workaround, can you try ?

var policy = new android.os.StrictMode.ThreadPolicy.Builder().permitAll().build();
android.os.StrictMode.setThreadPolicy(policy);

@dotnetdreamer dotnetdreamer added the help wanted Extra attention is needed label Feb 20, 2020
@Fr3nky88
Copy link

@wolfchkov a temp workaround, can you try ?

var policy = new android.os.StrictMode.ThreadPolicy.Builder().permitAll().build();
android.os.StrictMode.setThreadPolicy(policy);

It works for me as workaround.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants