okhttp3调用response.body().string();报错java.lang.IllegalStateException: closed

原因

okhttp3只允许调用response.body()一次,因为响应体可能很大,所以OkHttp没有存入响应体到内存,只是作为流从网络中读取。

原来代码中由于调试调用了两次:

  log.info(responseBody.string());
  int code = Integer.parseInt(responseBody.string());

可参考:
java.lang.IllegalStateException: closed when trying to access response in onResponse(Response response) · Issue #1240 · square/okhttp · GitHub

你可能感兴趣的:(okhttp3调用response.body().string();报错java.lang.IllegalStateException: closed)