fre*_*ptf 59 java android okhttp
我正在尝试使用OkHttp获取一些json数据,并且无法弄清楚为什么当我尝试记录response.body().toString()我得到的是Results:? com.squareup.okhttp.Call$RealResponseBody@41c16aa8
try {
URL url = new URL(BaseUrl);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header(/****/)
.build();
Call call = client.newCall(request);
Response response = call.execute();
**//for some reason this successfully prints out the response**
System.out.println("YEAH: " + response.body().string());
if(!response.isSuccessful()) {
Log.i("Response code", " " + response.code());
}
Log.i("Response code", response.code() + " ");
String results = response.body().toString();
Log.i("OkHTTP Results: ", results);
Run Code Online (Sandbox Code Playgroud)

我不知道我在这里做错了什么.我如何获得响应字符串?
V.J*_*.J. 208
您有使用.string()函数来打印响应System.out.println().但最后Log.i()你正在使用.toString().
因此,请使用.string()响应正文进行打印并获取您的请求的响应,例如:
response.body().string();
Run Code Online (Sandbox Code Playgroud)
注意:
.toString():这将以字符串格式返回您的对象.
.string():这会返回您的回复.
我认为这可以解决你的问题......对.
Ode*_*gev 27
以防有人碰到和我一样奇怪的事情.我在调试模式的开发过程中运行我的代码,显然是从OKHttp 2.4开始
..响应体是一次性值,只能消耗一次
因此,在调试时,检查员会在"幕后"调用并且正文始终为空.请参阅:https://square.github.io/okhttp/3.x/okhttp/okhttp3/ResponseBody.html
小智 9
该response.body,.string()可一次食用.请使用如下:
String responseBodyString = response.body.string();
use the responseBodyString as needed in your application.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64115 次 |
| 最近记录: |