java.lang.IllegalStateException: closed when trying to access response in onResponse(Response respo

来源:互联网 发布:ne80e端口物理层down 编辑:程序博客网 时间:2024/05/16 10:52

在使用okhttp成功接收json数据后进行打印log:
@Override
public void onResponse(Response response) throws IOException {
// Log.i(“log”, ” onResponse() reuslt=” + response.body().string());
JSONObject jsonobject =JSON.parseObject(response.body().string());
Log.d(“log”,”数据成功 码:”+jsonobject.get(“code”));
}

错误原因:由于某处两次都调用了response.body().string();
导致closed,有且只能调用一次


错:
09-13 17:41:22.763 19725-21188/mhwh.wow E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: mhwh.wow, PID: 19725
java.lang.IllegalStateException: closed
at com.squareup.okhttp.internal.http.HttpConnectionFixedLengthSource.read(HttpConnection.java:454)atokio.Buffer.writeAll(Buffer.java:574)atokio.RealBufferedSource.readByteArray(RealBufferedSource.java:87)atcom.squareup.okhttp.ResponseBody.bytes(ResponseBody.java:56)atcom.squareup.okhttp.ResponseBody.string(ResponseBody.java:82)atmhwh.wow.fragment.FragmentThreeTimeRecord1.onResponse(FragmentThreeTimeRecord.java:149)
at com.squareup.okhttp.CallAsyncCall.execute(Call.java:150)atcom.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)atjava.util.concurrent.ThreadPoolExecutorWorker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

0 0