解决方案:这是我这边的错误。
正确的方法是 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();
}
有人可以帮忙吗?
原文由 Haifeng Zhang 发布,翻译遵循 CC BY-SA 4.0 许可协议
示例添加到您的列: