您的位置:首页 > 理论基础 > 计算机网络

OkHttp请求回调中response.body().string()只能有效调用一次问题

2017-02-14 10:36 603 查看
其实主要是其.string()方法,内部 return new String(bytes(), charset().name())

每个ResponseBody实例只能读取一次,在实践中,因为没有自动缓冲发生。string()实际上是对输入的详尽读取,因此在后续调用底层数据源为空。

stack overflow的回答

The response body can be consumed only once.

This class may be used to stream very large responses. For example, it is possible to use this class to read a response that is larger than the entire memory allocated to the current process. It can even stream a response larger than the total storage on the current device, which is a common requirement for video streaming applications.

Because this class does not buffer the full response in memory, the application may not re-read the bytes of the response. Use this one shot to read the entire response into memory with bytes() or string(). Or stream the response with either source(), byteStream(), or charStream().
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  okhttp3
相关文章推荐