-1

在我的代码中 json vlaue not show 帮助我 worldpopulation vlaues 将显示在屏幕上,但 worldpopulation2 值不显示我的代码中的错误是什么?检查我的xml文件请帮我检查我的代码为什么worldpopulation1值显示y worldpopulation2值不显示???

         CategoryAPI = Utils.CategoryAPI2;


    try {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 
      15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(CategoryAPI);
        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");

            JSONObject person=(new JSONObject("worldpopulation2"));
             String Name=person.getString("describtion");
             String url2=person.getString("url");
             txtdescription.setText("json values are"+Name+""+url2);

            for (int i = 0; i < data.length(); i++) {
                JSONObject object = data.getJSONObject(i); 



                  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);


             }


    } 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();
     }  



}






                  {
"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"
}

]
,
"worldpopulation2": 
{
"rank":1,
"describtion": "Super mazaydar biryani",
 "url": "http://2.bp.blogspot.com/_JU_j7jj5TjU/TSBQKRukf1I/AAAAAAAAAs8/X1w5_z6pjwQ   
 /s1600/chicken-biryani.jpg"
 }



   }
4

1 回答 1

0

代替

JSONArray data = json.getJSONArray("worldpopulation");

JSONObject person=(new JSONObject("worldpopulation2"));

做这样的事情来从响应 JSON 数组创建新的 jsonObject。

 JSONArray data = json.getJSONArray("worldpopulation");

现在对 JSONArray 数据运行一个循环,看看你想从 JSON 对象中得到什么。

 for (int i = 0; i < data.length(); i++) {
    JSONObject person = data.getJSONObject(i);
    String population2 = person.getString("worldpopulation2");
   }
于 2013-07-24T11:27:22.917 回答