今天使用OkHttp的时候遇到一个坑,加了句log后就报错
Log.d(TAG, "onResponse: "+ response.body().string());
String resp = response.body().string();
2019-01-11 11:22:45.607 16904-17007/com.check.rocketchatmsgpush E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.check.rocketchatmsgpush, PID: 16904
java.lang.IllegalStateException: closed
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:407)
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:401)
at okhttp3.internal.Util.bomAwareCharset(Util.java:471)
at okhttp3.ResponseBody.string(ResponseBody.java:175)
at com.check.rocketchatmsgpush.util.RocketChat$1.onResponse(RocketChat.java:55)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
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)
后来查了一下,在这个博客 https://blog.csdn.net/ucxiii/article/details/52447945 找到了原因,原来是 response.body().string()
调用了多次导致的,string()
仅可调用一次。真心觉得坑。