我正在尝试使用 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);
我不知道我在这里做错了什么。我如何获得响应字符串?
原文由 freddieptf 发布,翻译遵循 CC BY-SA 4.0 许可协议
以防万一有人碰到和我一样奇怪的东西。我在 调试模式下 的开发过程中运行我的代码,显然是因为 OKHttp 2.4
因此,在调试时,检查员会在“幕后”进行调用,并且主体始终是空的。请参阅: https ://square.github.io/okhttp/3.x/okhttp/okhttp3/ResponseBody.html