解决方案: 这是我的错误。
正确的方式是使用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();
}
有人可以帮忙吗?
response.body().string()
替换成了response.body().toString()
... - Haifeng Zhang