java.lang.IllegalStateException: closed
摘要:
自定義Okhttp攔截器遇到的問題
當我們重寫Interceptor的intercept方法時,如果是攔截了返回(Response),呼叫Response的string()方法如下圖,原因是:response.body().string()只能請求一次,請求過後,就會...
自定義Okhttp攔截器遇到的問題
- 當我們重寫Interceptor的intercept方法時,如果是攔截了返回(Response),呼叫Response的string()方法如下圖,原因是:response.body().string()只能請求一次,請求過後,就會關閉,再次呼叫response.body().string()就會報close異常。
String content= response.body().string();
- 解決方案:重新builder一個Response ,重新設定一個response。
Response response = chain.proceed(newRequest); MediaType mediaType = response.body().contentType(); String content= response.body().string(); LogUitls.e("tag", content); return response.newBuilder() .body(ResponseBody.create(mediaType, content)) .build();