当我尝试记录网络响应时,OKHttp抛出非法状态异常

问题描述 投票:14回答:4

我在我的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)

我看到堆栈中有多个拦截器,但我只是明确地添加了一个拦截器,这就是抛出异常。

java android retrofit okhttp
4个回答
28
投票

您正在使用拦截器中的响应主体,因此您将要创建一个新响应:

@Override public Response intercept(Chain chain) throws IOException {
  Response response = chain.proceed(chain.request());
  ResponseBody body = response.body();
  String bodyString = body.string();
  MediaType contentType = body.contentType();
  Log.d("Response", bodyString);
  return response.newBuilder().body(ResponseBody.create(contentType, bodyString)).build();
}

您可能还想查看OkHttp的repo:https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor中的日志拦截器


17
投票

不要多次调用响应主体,因为它被读取为流而不是存储在内存中。

也许你不止一次地调用response.body()。string(),因为响应体可能很大,所以OkHttp不会将它存储在内存中,它会在需要时将其作为来自网络的流读取。

当你读取body作为字符串时()OkHttp下载响应体并将其返回给你而不保持对字符串的引用,如果没有新的请求则无法下载两次。

https://github.com/square/okhttp/issues/1240


3
投票

由于尝试转换响应体2X,我也得到了这个例外。首先我记录了response.body()。string()然后我又将其传递给了一个方法。这导致了例外。检查响应主体是否多次转换为字符串。


0
投票

错误很奇怪,但我看到的第一个问题是您正在使用OKHTTP3并尝试将构建器添加到已创建的客户端。

OkHttpClient现在是不可变的,您必须通过在构建器上调用addInterceptor方法将拦截器直接添加到OkHttpClient.Builder。

有关如何执行此操作的更清晰,请参阅此Github Issue。我认为这应该可以解决你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.