无法获取OkHttp的response.body.toString()来返回一个字符串 [英] Can't get OkHttp's response.body.toString() to return a string
问题描述
我正在尝试使用OkHttp获取一些json数据,并且无法弄清楚为什么当我尝试记录 response.body()。toString()
我是什么get是 Results :: com.squareup.okhttp.Call$RealResponseBody@41c16aa8
I'm trying to get some json data using OkHttp and can't figure out why when i try logging the response.body().toString()
what i get is 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);
我不知道我在这里做错了什么。我如何获得响应字符串?
I don't know what i'm doing wrong here. How do i get the response string?
推荐答案
您使用 .string()
函数在 System.out.println()
中打印响应。但最后在 Log.i()
中你使用 .toString()
。
You have use .string()
function to print the response in System.out.println()
. But at last in Log.i()
you are using .toString()
.
因此请在响应正文中使用 .string()
进行打印并获取您的请求回复,例如:
So please use .string()
on response body to print and get your request's response, like:
response.body().string();
注意:
-
.toString()
:这将以字符串格式返回您的对象。
.toString()
: This returns your object in string format.
.string()
:这会返回您的回复。
.string()
: This returns your response.
我认为这可以解决您的问题......对。
I think this solve your problem... Right.
这篇关于无法获取OkHttp的response.body.toString()来返回一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!