请检查我的代码这是访问 json 数组的正确方法吗?将显示数组“worldpopulation”数据,但不会显示数组“联系人”数据检查我的代码 mi 是否正确访问?????????? 我的完整代码在这里我只是在我的 json 文件“联系人”中添加新数组并访问它请检查我的代码
{
"worldpopulation": [
{
"rank":1,
"name": "Angelina",
"url": "http://www.bounty4u.com/android/images/angie.jpg"
},
{
"rank":2,
"name": "Ashton ",
"url": "http://www.bounty4u.com/android/images/ashton.jpg"
},
{
"rank":3,
"name": "Jackman",
"url": "http://www.bounty4u.com/android/images/hugh.jpg"
}
]
,
"contacts": [
{
"id":1,
"Item": "Calories",
"Quantity": "150g"
},
{
"id":2,
"Item": "Calcium",
"Quantity": "250g"
},
{
"id":3,
"Item": "Carbohydrates",
"Quantity": "300g"
}
]
}
try {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
HttpConnectionParams.setSoTimeout(client.getParams(),
15000);
HttpUriRequest request = new HttpGet(DescriptionAPI);
HttpResponse response = client.execute(request);
InputStream atomInputStream =
response.getEntity().getContent();
BufferedReader in = new BufferedReader(new
InputStreamReader(atomInputStream));
String line;
String str = "";
while ((line = in.readLine()) != null){
str += line;
}
JSONObject json = new JSONObject(str);
JSONArray data =
json.getJSONArray("worldpopulation");
for (int i = 0; i < data.length(); i++) {
JSONObject object = data.getJSONObject(i);
// JSONObject category =
object.getJSONObject("Category");
Category_ID.add(Long.parseLong(object.getString("rank")));
Category_name.add(object.getString("name"));
Category_image.add(object.getString("url"));
Log.d("Category name", Category_name.get(i));
listview.setAdapter(cla);
}
////Acess Json array Contacts/////////////////////
JSONArray data2 = json.getJSONArray("contacts");
final TableLayout table = (TableLayout)
findViewById(R.id.table2);
for (int j = 0; j < data2.length(); j++)
{
final View row = createRow(data2.getJSONObject(j));
table.addView(row);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
// IOConnect = 1;
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public View createRow(JSONObject item) throws JSONException {
View row = getLayoutInflater().inflate(R.layout.rows, null);
((TextView) row.findViewById(R.id.localTime)).setText(item
.getString("Item"));
((TextView) row.findViewById(R.id.apprentTemp)).setText(item
.getString("Quantity"));
return row;
}