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().
每个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().
相关文章推荐
- OkHttp请求回调中response.body().string()只能有效调用一次,为什么?
- OkHttp请求回调中response.body().string()只能有效调用一次
- OkHttp踩坑随笔为何 response.body().string() 只能调用一次
- OkHttp请求回调中response.body().string()如何调用两次
- OkHttp中 response.body().string()只能调用一次分析~~~
- 为何 response.body().string() 只能调用一次
- OkHttp踩坑记:为何 response.body().string() 只能调用一次?
- okhttp无法获取response.body().string()的问题解决
- okhttp发送post(json)请求数据,出现不走response.body().string(),单起一行打印的异常
- 使用AJAX调用Spring controller 并返回map对象,@ResponseBody 只能返回string的解决方案:
- Okhttp拦截器统一异常处理并多次读取response.body().string()
- 解决springmvc ResponseBody请求返回406的问题
- Spring @ResponseBody只能返回String类型数据解决办法
- Fiddler获取ResponseBody乱码问题解决方法----oSession.GetResponseBodyAsString();
- js-window.onload只能调用一次的问题!
- springmvc Spring3 MVC @ResponseBody返回,jquery ajax调用中文乱码问题解决
- springmvc 请求参数中文乱码 ,@ResponseBody 返回中文乱码问题
- springmvc Spring3 MVC @ResponseBody返回,jquery ajax调用中文乱码问题解决
- IE8浏览器缓存问题导致Ajax的GET请求只能执行一次的解决办法
- WCF ajax调用,同一浏览器窗口的多个请求在服务端阻塞,只能顺序执行的问题