本文介绍了一个OkHttp响应访问体中的字符串两次导致IllegalStateException中:关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现通过OkHttp库我的HTTP调用。一切工作正常,但我注意到,当我进入身体两倍的 IllegalStateException异常将被抛出响应的字符串。也就是说,我做的(例如): Log.d(TAG,response.body()字符串())之后,我确实希望使用字符串如 processResponse(response.body()。字符串())。但是,第二个电话的消息关闭引发异常。

I implement my http calls via the OkHttp library. Everything works fine, but I noticed that, when I access the body as a string of the response twice an IllegalStateException will be thrown. That is, I do (for example): Log.d("TAG", response.body().string()) and after that I actually want to use that string like processResponse(response.body().string()). But that second call throws the exception with the message closed.

怎么可能是两次访问字符串导致失败?我想处理,而不需要的响应添加一个包装/虚拟对象只是为了节省一些值(如标题,正文,状态code)。

How can it be possible that accessing a string twice results in a failure? I want to process that response without the need to add a wrapper/dummy object just for saving some values (like header, body, statuscode).

推荐答案

在响应字符串方法读取输入的(网络)数据流,并将其转换成字符串。因此,动态生成的字符串,并将其返回给你。你怎么称呼它第二次,网络流已经被消耗掉,不再提供。

The string method on the response will read the input (network) stream and convert it into a string. So it dynamically builds the string and returns it to you. The second time you call it, the network stream has already been consumed and is no longer available.

您应该根据需要对字符串的结果保存到一个字符串变量,然后访问它多次。

You should save the result of string into a String variable, and then access it as many times as needed.

这篇关于一个OkHttp响应访问体中的字符串两次导致IllegalStateException中:关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 23:48