user2134412
user2134412

Reputation:

Get the Json Array data in listview

i got a problem with json array data, i have tried the following code using that i can get the json response perfectly but in listview i am getting single data even in response also i am getting single item why?

here is the code:

URL = "some url";

HttpClient mHttpClient = new DefaultHttpClient();
HttpGet mGetMethod = new HttpGet(URL);
HttpResponse mReponseMessage = mHttpClient.execute(mGetMethod);

String Response = EntityUtils.toString(mReponseMessage.getEntity());
Log.d("TAG", "O/P Response is " + Response);

JSONArray responseObject = new JSONArray(Response);
System.out.println("responseObject="+responseObject);

for(int i=0; i<responseObject.length(); i++) {                      
    obj = responseObject.getJSONObject(i);
}

here is my json response

[{"dmessage":"sfsfs","message":"sfsf","mp3":"Kalimba.mp3","user_message_id":"85","category":"Lottery","title":"dgfs"},{"dmessage":"prueba","message":"prueba","mp3":"NA","user_message_id":"80","category":"Lottery","title":"prueba"},{"dmessage":"prueba","message":"prueba","mp3":"NA","user_message_id":"79","category":"Lottery","title":"prueba"},

Here obj is response object,in response object also i am getting the single value

Could anybody help me to solve the issue Thanks!

Upvotes: 0

Views: 407

Answers (3)

Linga
Linga

Reputation: 10553

You need to parse properly. First convert the response into String and then parse it as JSON object.

Use this:

HttpClient mHttpClient = new DefaultHttpClient();
HttpGet mGetMethod = new HttpGet(URL);
HttpResponse mReponseMessage = mHttpClient.execute(mGetMethod);
HttpEntity entity = mResponseMessage.getEntity();
InputStream is = entity.getContent();

and then

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
}
is.close();
String json = sb.toString();
}
catch(Exception e)
{
 // 
}

Next,

JSONObject jObj = new JSONObject(json);
responseObject=JObj.getJSONArray("array_name");// if passed as an array
for(int i=0; i<responseObject.length(); i++) {                      
JSONObject obj = responseObject.getJSONObject(i);
String dmessage,message,mp3,user_message_id,category,title;
dmessage= obj.getString("dmessage");
message= obj.getString("message");
mp3= obj.getString("mp3");
user_message_id= obj.getString("user_message_id");
category= obj.getString("category");
title= obj.getString("title");

// here you can add it to the list
}

Upvotes: 0

Sankar V
Sankar V

Reputation: 4863

In your for loop you are looping through all the values in the array and storing it in the same variable which replaces the value each time and contains only the last value.

Solution :

ArrayList<HashMap<String, String, String, String, String, String>> mp3List = new ArrayList<HashMap<String, String, String, String, String, String>>();
for(int i=0; i<responseObject.length(); i++) 
{                      
    JSONObject obj = responseObject.getJSONObject(i);
    String dmessage= obj.getString("dmessage");
    String message= obj.getString("message");
    String mp3= obj.getString("mp3");
    String userMessageID= obj.getString("user_message_id");
    String category= obj.getString("category");
    String title= obj.getString("title");

    //making use of obtained strings by adding it to some ArrayList to display in the ListView

    HashMap<String, String, String, String, String, String> map = new HashMap<String, String, String, String, String, String>();

     // adding each child node to HashMap key => value
     map.put("dmessage", dmessage);
     map.put("message", message);
     map.put("mp3", mp3);
     map.put("userMessageID", userMessageID);
     map.put("category", category);
     map.put("title", title);

     // adding HashList to ArrayList
     mp3List.add(map);
}

//you have all the data in mp3List. Display it in ListView
ListAdapter adapter = new SimpleAdapter(this, mp3List, custom_listitem_layout_id, new String[] { "dmessage", "message", "mp3", "userMessageID", "category", "title"}, new int[] { dmessage_textview_id, message_textview_id, mp3_textview_id, userMessageID_textview_id, category_textview_id,title_textview_id });
listview.setAdapter(adapter);

Upvotes: 3

Tamilselvan Kalimuthu
Tamilselvan Kalimuthu

Reputation: 1532

Have a look at this and get some clear idea about parsing

JsonParser

Upvotes: 0

Related Questions