0
点赞
收藏
分享

微信扫一扫

在OKHttpClient的拦载器里遇到java.lang.IllegalStateException异常


最近发现当请求结果返回400,400,500等之类的错误时,总是报一个错:java.lang.IllegalStateException: closed

后来在这篇文章​​https://github.com/square/retrofit/issues/3336​​里找到了答案。

正常文章里提到了,我真的就是用了(注:Kotlin写的android程序):

val bodyContent = response.body?.string()
response.body?.close()

如JakeWharton 所说的那样,我在interceptor里从response读取内容并关闭response。这样将导致Retrofit里无法正常使用response,如报的错提示那样:

java.lang.IllegalStateException: closed

解决方法就是用OkHttp的 peekBody()来代替上面的做法:

response.peekBody(Long.MAX_VALUE).string()

借此记录一个错误。


举报

相关推荐

0 条评论