java.lang.IllegalStateException: closed

使用okhttp3的时候遇到的异常:java.lang.IllegalStateException: closed。

原因是流已经关闭,所以无法再进行操作。

问题出现在我在callback中调用了两次response.body().string(),导致了错误的出现!

//onResponse还是在子线程中执行
@Override
public void onResponse(Call call,Response response) throws IOException {
    L.e("onResponse:"+response.body().string());
    final String result=response.body().string();
   
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mTvResult.setText(result);
        }
    });
}

OkHttp请求回调中response.body().string()只能有效调用一次,调用response.body().string()的时候数据流已经关闭了,再次调用就是提示已经closed。然后我把代码修改完之后就可以了,只调用一次response.body().string();

}
//onResponse还是在子线程中执行
@Override
public void onResponse(Call call,Response response) throws IOException {
    final String result=response.body().string();
     L.e("onResponse:"+result);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mTvResult.setText(result);
        }
    });
}

猜你喜欢

转载自blog.csdn.net/wen_haha/article/details/81018046
今日推荐