IllegalStateException: closed okhttp報錯
阿新 • • 發佈:2019-01-06
IllegalStateException: closed okhttp報錯
今天使用http呼叫介面報錯 IllegalStateException: closed
2019-01-06 13:51:24.729 ERROR 8364 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: closed] with root cause java.lang.IllegalStateException: closed at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:377) at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:371) at okhttp3.internal.Util.bomAwareCharset(Util.java:412) at okhttp3.ResponseBody.string(ResponseBody.java:173)
檢視程式碼
//判斷請求是否成功 int status = response.code(); logger.debug("請求狀態碼為:" + status); if(response.isSuccessful()){ //列印服務端返回結果 logger.debug("請求成功結果為:" + response.body().string()); return response.body().string(); }
可以看到 response.body().string() 出現了兩次 , **response.body().string()只能請求一次,**請求過後,就會關閉,再次呼叫response.body().string()就會報close異常,所以String data = response.body().string();
使用變數接收資料,就可以解決這個問題。