OKHttp 在我尝试记录网络响应时引发非法状态异常
我在我的OkHttp客户端上放置了以下拦截器:
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
Log.d("Response", response.body().string());
return response;
}
});
但是,这在Retrofit 2中并不好。似乎您只能从响应中读取一次流,这可能是导致异常的原因。我认为改造是试图解析日志已经解析的流。那么,我如何获得响应?我目前正在尝试调试一个非常讨厌和奇怪的格式错误的json异常。
这是异常堆栈跟踪:
07 - 28 10: 58: 21.575 22401 - 22529 / REDACTED E / AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: REDACTED, PID: 22401
java.lang.IllegalStateException: closed
at okhttp3.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java: 378)
at okio.Buffer.writeAll(Buffer.java: 956)
at okio.RealBufferedSource.readByteArray(RealBufferedSource.java: 92)
at okhttp3.ResponseBody.bytes(ResponseBody.java: 83)
at okhttp3.ResponseBody.string(ResponseBody.java: 109)
at REDACTED.ServiceGenerator$2.intercept(ServiceGenerator.java: 90)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java: 187)
at REDACTED.ServiceGenerator$2.intercept(ServiceGenerator.java: 89)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java: 187)
at REDACTED.ServiceGenerator$2.intercept(ServiceGenerator.java: 89)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java: 187)
at REDACTED.ServiceGenerator$2.intercept(ServiceGenerator.java: 89)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java: 187)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java: 160)
at okhttp3.RealCall.access$100(RealCall.java: 30)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java: 127)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java: 32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 587)
at java.lang.Thread.run(Thread.java: 841)
我看到堆栈中有多个拦截器,但我只显式添加一个拦截器,这是引发异常的on。