OkHttp GET 和读取 body 时阻塞的可能性

OkHttp GET and the possibility of blocking when reading body

OkHttp 的 recipes page 声明在发出异步请求时,

The callback is made after the response headers are ready. Reading the response body may still block.

什么情况下读取响应体会阻塞?阻塞的可能性不会破坏进行异步调用的全部目的吗?

用过OkHttp的朋友们,你们是怎么处理可能被封的?

  1. 忽略它。
  2. 使用同步请求并自己使它们异步,例如,在 AsyncTask.
  3. 别再浪费时间了,使用不同的 HTTP 库。
  4. ???

Under what conditions would reading the response body block?

当读取headers后body大于剩余的space时headers使用的2k块大小

Doesn't the possibility of blocking defeat the whole purpose of making an asynchronous call?

没有。没有人愿意编写处理字节处理的异步代码。

收到响应后,我们会向您提供以阻塞方式从中读取数据的来源。阻塞 IO 代码更易于阅读、编写和推理,并且此 space 中的大多数库(如 Gson)都设置为阻塞读取。

For those who have used OkHttp, how did you deal with the possibility of blocking?

您只需阻塞读取 body 数据,然后在完成时通过您想要的任何机制通知应用程序代码。

你提到了 AsyncTask,这让你看起来像是在 Android。回调不会在主线程上发生,所以阻塞 IO 并不重要。