解决OKHttp报java.lang.IllegalStateException closed

原因:OkHttp请求回调中response.body().string()只能有效调用一次

在调用response.body().string()方法之后,response中的流就会被关闭,如果我们需要创建出一个新的response给应用层处理。

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    logForRequest(request);
    Response response = chain.proceed(request);
    MediaType mediaType = response.body().contentType();
    String content = response.body().string();
    Log.i(TAG, content);
    return response.newBuilder()
            .body(ResponseBody.create(mediaType, string))
            .build();
}
上一篇 Glide 4.x找不到AppGlideModule
下一篇 Multiple dex files define解决记录
目录
文章列表
1 shell脚本报错:/bin/bash^M bad interpreter No such file or directory
shell脚本报错:/bin/bash^M bad interpreter No such file or directory
2
ElasticSearch 7 搜索(6)trem多种查询
ElasticSearch 7 搜索(6)trem多种查询
3
Eureka服务注册是采用主机名还是IP地址
Eureka服务注册是采用主机名还是IP地址
4
Spring Boot集成XStream转换器:处理xml节点中既有属性又有值
Spring Boot集成XStream转换器:处理xml节点中既有属性又有值
5
Magento 2日志打印
Magento 2日志打印
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。