解决Retrofit2 Okhttp3 返回ResponseBody出现java.lang.IllegalStateException: closed

09-05 14:42:17.606: E/AndroidRuntime(28219): Caused by: java.lang.IllegalStateException: closed
09-05 14:42:17.606: E/AndroidRuntime(28219): at okhttp3.internal.http.Http1xStream ChunkedSource.read(Http1xStream.java:414)090514:42:17.606:E/AndroidRuntime(28219):atokio.Buffer.writeAll(Buffer.java:993)090514:42:17.606:E/AndroidRuntime(28219):atokio.RealBufferedSource.readByteArray(RealBufferedSource.java:106)090514:42:17.606:E/AndroidRuntime(28219):atokhttp3.ResponseBody.bytes(ResponseBody.java:128)090514:42:17.606:E/AndroidRuntime(28219):atokhttp3.ResponseBody.string(ResponseBody.java:154)090514:42:17.606:E/AndroidRuntime(28219):atcom.example.nettest.MainActivity MyAsyncTask.doInBackground(MainActivity.java:78)
09-05 14:42:17.606: E/AndroidRuntime(28219): at com.example.nettest.MainActivity MyAsyncTask.doInBackground(MainActivity.java:1)090514:42:17.606:E/AndroidRuntime(28219):atandroid.os.AsyncTask 2.call(AsyncTask.java:292)
09-05 14:42:17.606: E/AndroidRuntime(28219): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-05 14:42:17.606: E/AndroidRuntime(28219): … 4 more

基于okhttp3的返回对象ResponseBody ,其中的string();只可调用一次,调用完毕会执行closed相关方法.再调用会报错, 切记!!

你可能感兴趣的:(Android技术集锦,问题集锦)