美文网首页
解决okhttp报java.lang.IllegalState

解决okhttp报java.lang.IllegalState

作者: Hardy小叶 | 来源:发表于2017-02-21 13:24 被阅读808次

原因为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.e("tag", content);
return response.newBuilder()
.body(ResponseBody.create(mediaType, string))
.build();
// return logForResponse(response);

相关文章

网友评论

      本文标题: 解决okhttp报java.lang.IllegalState

      本文链接:https://www.haomeiwen.com/subject/hxfuwttx.html