解决okhttp报java.lang.IllegalStateException: closed,java.lang.IllegalStateException: closed

解决okhttp报java.lang.IllegalStateException: closed,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.e("tag", content);
        return response.newBuilder()
                .body(ResponseBody.create(mediaType, string))
                .build();
      // return logForResponse(response);

猜你喜欢

转载自blog.csdn.net/qq_25749749/article/details/79757900