Callback failure for canceled call in OkHttp

别说谁变了你拦得住时间么 提交于 2019-12-06 06:31:36

It is calling your onResponse method. It appears in the stack trace as

 at hr.artplus.httptestm.MainActivity$2.onResponse(MainActivity.java:87) 

What is happening is that the you are getting an IOException on this line --

 final byte[] input = response.body().bytes();

probably because the stream is closed while you were reading it due to the cancellation. Your code lets the exception propagate back up to the Callback class, which does not issue another callback because it already called onResponse.

You can catch the exception and deal with it in your callback --

if (response.isSuccessful()) {
   try { 
       final byte[] input = response.body().bytes();
       final int len = (int) response.body().contentLength();
   } catch (IOException e) {
       // Signal to the user failure here, re-throw the exception, or 
       // whatever else you want to do on failure
   }
   ...                    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!