【发布时间】:2015-03-29 01:58:58
【问题描述】:
解决方案: 这是我这边的一个错误。
正确的方法是 response.body().string() 而不是 response.body.toString()
我使用Jetty servlet,URL是http://172.16.10.126:8789/test/path/jsonpage,每次请求这个URL都会返回
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
在浏览器中输入 url 时显示,不幸的是,当我使用 Okhttp 请求时,它显示的内存地址不是 json 字符串。
TestActivity﹕ com.squareup.okhttp.internal.http.RealResponseBody@537a7f84
我正在使用的 Okhttp 代码:
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
谁能帮忙?
【问题讨论】:
-
它从 char 字节和字符集构造 String 对象。
-
@NikolaDespotoski 谢谢!这是我自己的错误。我将
response.body().string()替换为response.body().toString()... -
@haifzhan 感谢您澄清使用 response.body().string() 而不是 response.body().toString()
标签: java http okhttp embedded-jetty