正在从API获取2d数组,正在解析响应。但是我无法获得这些价值。
这就是得到回应的方式,
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
String api_uri = "http://192.168.0.106/api/something/";
HttpGet httpget = new HttpGet(api_uri);
httpget.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
Log.e("rAct", result);
} catch (Exception e) {
// Oops
Log.e("rActEr", e.toString());
} finally {
try {
if (inputStream != null) inputStream.close();
} catch (Exception squish) {}
}
该result字符串如下,
[
[
{
"id": 138,
"name": "asdasd",
"person_image": "http://something.in:9090/photos/14/1575.jpg",
"person_description": "bla bla blaa",
"bla_id": 1,
"blabal_id": 2,
"other_id": 7,
"category": "asd"
}
],
[
{
"id": 257,
"name": "asdasd",
"person_image": "http://something.in/asd.jpg",
"person_description": "asasdsad",
"bla_id": 1,
"blabal_id": 2,
"other_id": 7,
"category": "ASSSD"
}
]
]
所以现在尝试读取数据,
try {
jsonarray = new JSONArray(result);
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Log.e("testt", jsonobject.toString());
...
...
}
}catch....
我不能够从该得到什么jsonobject 如何获得的name,person_image,person_description从结果数据。我试图让他们喜欢jsonobject.optString("name"),但什么也没回来。
使用一维数组,我得到了响应,并且可以按照上面相同的方式读取数据。
我需要修改什么才能读取所需的数据?
杨__羊羊
GCT1015
相关分类