0

In my android application I'm receiving an JSONArray now I should parse that and should show the data in the listview. I tried it and it is showing '{}' in all list items in listview.Cannot understand why its showing like that.

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



    }); 

}  
2
  • Show your json response. Also check that url must be correct. Commented Apr 24, 2015 at 6:02
  • @Piyush Gupta url is correct only only thing is parsing is wrong I'm thinking Commented Apr 24, 2015 at 6:13

1 Answer 1

1

The error is due to these lines :

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

Here you are not initializing the JSONObject from JSONArray , but you are creating a new one.

The solution could be to initialize it from JSONArray :

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

For parsing thing In your JSON nested JSONArrays are given.

you can try following code to parse it:

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);
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

then please post your JSON response.
actually what I'm trying to do is in my php script I'm sending an array as a response now my problem is to get the array and parse it.
Yes thats what I am asking. Post your response data the same data that you are using here JSONArray arr = new JSONArray(response);
[1],[1][2],[1][2][3],[1],[2],[3],[4] its showing like this in the list items in the listview
So its showing data in listview. I think that was your problem. Its resolved now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.