私のコードを確認してください。json配列にアクセスする正しい方法ですか? 配列 "worldpopulation" データは表示されますが、配列 "contacts" データは表示されません 私のコードを確認して正しくアクセスしてください???????????? 私の完全なコードはここにあります.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;
}