最近發現當請求結果傳回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()