在android中解析JsonArray无法正常工作

时间:2015-04-24 05:58:20

标签: android json listview

在我的Android应用程序中,我现在收到JSONArray,我应该解析它并且应该在listview中显示数据。我试过了,它正在显示' {}'在listview中的所有列表项中。无法理解为什么它会显示出来。

    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    params.put("topicsJSON",composeJSON());
    client.post("http://www.example.com/LSM_Sci-Mat/load_topics.php",params,new AsyncHttpResponseHandler()
    {

        public void onSuccess(String response)
        {


            Gson gson = new GsonBuilder().create();
            try 
            {

                JSONArray arr = new JSONArray(response);

                for (int i = 0; i < arr.length(); i++) 
                {


                    JSONObject jsonObject = new JSONObject();
                    list.add(jsonObject.toString());
                    load_data();
                    //Toast.makeText(getApplicationContext(), "Element ==> "+list, 5000).show();

                }

            }
            catch (JSONException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

                }

        @Override
        public void onFailure(int statusCode, Throwable error,String content)

        {

            if (statusCode == 404) 

            {
                Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
            } 

            else if (statusCode == 500) 

            {
                Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
            } 

            else 

            {
                Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
                        Toast.LENGTH_LONG).show();
            }
        }

    });



}

private String composeJSON() {
    // TODO Auto-generated method stub
    ArrayList<HashMap<String, String>> check_in_List;
    check_in_List = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();

    map.put("subject_code",received_subject_code);


    check_in_List.add(map);
    Gson gson = new GsonBuilder().create();
    return gson.toJson(check_in_List);

}

public void load_data()
{
    ArrayAdapter<String> phy = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);

    l1.setAdapter(phy);

    l1.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            // When clicked, show a toast with the TextView text 

            //String pos = (String) l1.getItemAtPosition(position);
            int topic_index = position + 1;
            String pos = String.valueOf(topic_index);



    }); 

}  

1 个答案:

答案 0 :(得分:1)

错误是由于以下几行:

JSONObject jsonObject = new JSONObject();
list.add(jsonObject.toString());

这里你没有从JSONArray初始化JSONObject,但是你正在创建一个新的。

解决方案可能是从JSONArray初始化它:

JSONObject jsonObject = arr.getJSONObject(i)
list.add(jsonObject.toString());

解析事物在你的JSON中给出了嵌套的JSONArrays。

您可以尝试使用以下代码进行解析:

JSONArray arr = new JSONArray(response);        
for (int i = 0; i < arr.length(); i++) {  
  JSONArray internalJSONArray=arr.getJSONArray(i);
  for (int j=0;j<internalJSONArray.length();j++){
    String data=internalJSONArray.getString(j);
    System.out.println(data);
  }
}
相关问题