Android开发之Okhttp:java.lang.IllegalStateException: closed

在使用Okhttp的时候

运行到response.body().string()一步时抛异常,java.lang.IllegalStateException: closed

查阅各种资料大致意思是The IllegalStateException arises because the HttpConnection seems to be closed when trying to use it. Could it be because you are calling twice the method response.body()?

就是说调用response.body().string()的时候数据流已经关闭了,再次调用就是提示已经closed,

检查代码发现,其他的地方并没有调用过response.body().string(),而且是调试的时候有,后来发现是在debug的时候添加了Watchs,就是代码的监视,这里会调用一次response.body().string()。

后面需要注意,代码调试的时候表达式的监视有时候会影响代码的运行。

原文地址:https://www.cnblogs.com/liyiran/p/5498417.html