在Android上的okhttp上得到一个错误,应为null,但却是okhttp3.internal.http1.Htt



我得到了这个错误。在从api获得响应之后。(我认为当应用程序读取响应时,因为它与Http1Codec有关(。有人知道吗?它也很难繁殖。

(我们使用okhttp和ApoloGraphQL(

Fatal Exception: java.lang.IllegalStateException
expected null but was okhttp3.internal.http1.Http1Codec
okhttp3.internal.connection.StreamAllocation.streamFinished (StreamAllocation.java:304)
okhttp3.internal.http1.Http1Codec$AbstractSource.endOfInput (Http1Codec.java:386)
okhttp3.internal.http1.Http1Codec$FixedLengthSource.read (Http1Codec.java:416)
okio.RealBufferedSource.request (RealBufferedSource.java:68)
okhttp3.logging.HttpLoggingInterceptor.intercept (HttpLoggingInterceptor.java:241)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:147)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:121)
okhttp3.RealCall.getResponseWithInterceptorChain (RealCall.java:200)
okhttp3.RealCall$AsyncCall.execute (RealCall.java:147)
okhttp3.internal.NamedRunnable.run (NamedRunnable.java:32)

如果您的监视列表中有response.body((.string((,请小心,这将导致您的代码失败。由于监视列表将自动运行。

ResponseBody responseBodyCopy = response.peekBody(Long.MAX_VALUE);
responseBodyCopy.string();

当我们使用response.errorBody((或response.body((时,它将使用缓冲区。因此,在我们获得字符串之前,该值可能会消失

代替response.errorBody().toString

我们最好使用

ResponseBody errorBody = Objects.requireNonNull(response.errorBody());
String errorString = errorBody.toString();
onFailure(new ApiError(response.code(), errorString));

最新更新