天天看点

在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      
response.peekBody(Long.MAX_VALUE).string()