使用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);
}
});
}