java.lang.IllegalStateException: closed

自定义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();
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容