解:
我这是一个错误。
正确的方法是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代码Im使用:
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字节和charset构造String对象。
@NikolaDespotoski谢谢! 这是我自己的错误。 我将response.body().string()替换为response.body().toString() ...
@haifzhan感谢您澄清使用response.body()。string()而不是response.body()。toString()
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(urls[0])
.build();
Response responses = null;
try {
responses = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
String jsonData = responses.body().string();
JSONObject Jobject = new JSONObject(jsonData);
JSONArray Jarray = Jobject.getJSONArray("employees");
for (int i = 0; i < Jarray.length(); i++) {
JSONObject object = Jarray.getJSONObject(i);
}
}
示例添加到您的列:
JCol employees = new employees();
colums.Setid(object.getInt("firstName"));
columnlist.add(lastName);
我现在收到异常:org.json.JSONException: Value okhttp3.internal.http.RealResponseBody@aeb56f3 of type java.lang.String cannot be converted to JSONObject
一个非常简单的错误是将.string()与.toString()混淆
您应该指定从何处获取对象,例如JSONObject。请,也包括进口。
我试图使用Okhttp(3.x.x)进行简单的请求,这是基本的请求吗?
我也面临同样的问题
使用此代码:
// notice string() call
String resStr = response.body().string().toString();
JSONObject json = new JSONObject(resStr);
它肯定有效
非常感谢!有趣的是,文档中没有关于string()方法的信息。但实际上有效
非常感谢您的回答。这对我帮助很大!奇怪我在互联网上其他任何地方都找不到它!
非常感谢你,它对string()很奇怪,但是效果很好!
对于包含data和error对象的OKHttp3 GraphQL响应,这也适用。
添加toString()是否多余?您只需要response.body()。string()。返回String。
正如我在代码中观察到的那样。
如果一旦从"响应"中获取主体的值,则该值将变为空白。
String str = response.body().string(); // {response:[]}
String str1 = response.body().string(); // BLANK
因此,我相信,一旦从主体获取了价值,它就会变空。
建议:将其存储在String中,可以多次使用。
非常感谢。一直试图弄清楚这一点,上述答案均无济于事,因为我先记录了整个身体,然后尝试获取字符串,这实际上是问题所在。
我希望您设法从json字符串中获取json数据。
好吧,我认为这会有所帮助
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(urls[0])
.build();
Response responses = null;
try {
responses = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
String jsonData = responses.body().string();
JSONObject Jobject = new JSONObject(jsonData);
JSONArray Jarray = Jobject.getJSONArray("employees");
//define the strings that will temporary store the data
String fname,lname;
//get the length of the json array
int limit = Jarray.length()
//datastore array of size limit
String dataStore[] = new String[limit];
for (int i = 0; i < limit; i++) {
JSONObject object = Jarray.getJSONObject(i);
fname = object.getString("firstName");
lname = object.getString("lastName");
Log.d("JSON DATA", fname +" ##" + lname);
//store the data into the array
dataStore[i] = fname +" ##" + lname;
}
//prove that the data was stored in the array
for (String content ; dataStore ) {
Log.d("ARRAY CONTENT", content);
}
请记住使用AsyncTask或SyncAdapter(IntentService),以防止出现NetworkOnMainThreadException
还要在您的build.gradle中导入okhttp库
compile 'com.squareup.okhttp:okhttp:2.4.0'
我可以同时使用DownloadManager吗?
如果您注意到,下载文件后,状态栏中将出现一个闪烁的白色向下箭头。表示您下载了一些内容。那是DownloadManager的句柄