问题描述
使用okhttp请求数据,进行解析,遇到java.lang.IllegalStateException: closed
异常,错误堆栈如下
2022-02-21 15:28:17.537 14686-14972/com.xxE/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.xx, PID: 14686
java.lang.IllegalStateException: closed
at okio.RealBufferedSource.select(RealBufferedSource.java:93)
at okhttp3.internal.Util.bomAwareCharset(Util.java:467)
at okhttp3.ResponseBody.string(ResponseBody.java:181)
at com.xx.HealthHeroView$2.onResponse(HealthHeroView.java:211)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
at okhttp3.internal.NamedRunnable.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)
问题分析
response.body().string()只能请求一次,请求过后,就会关闭,再次调用response.body().string()就会报close异常,
问题解决
String data = response.body().string();
使用变量接收数据,后续使用变量data 就可以解决这个问题。