0

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!

6
  • What data you need to show it in the ListView? Commented Apr 20, 2013 at 6:45
  • yes .i want to show it in listview Commented Apr 20, 2013 at 6:47
  • I have edited my answer. Surely it will meet your need Commented Apr 20, 2013 at 6:57
  • But in arraylist i am getting the repeated last item Commented Apr 20, 2013 at 7:00
  • Really it shouldn't. I am afraid that you did something wrong. update your question with latest code. Without knowing the code we couldn't help Commented Apr 20, 2013 at 7:06

3 Answers 3

3

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

6 Comments

check this tutorial because we don't know anything about your json response. this will help you
@ Sankar V actually i have only array of item ..could you please figure it where is my mistake how could i make it right!!
Sure. update your question with json response. Kindly remove personal informations if any.
using that also i am getting single item only the last item only displaying!!
Thanks but only the last id is getting in arraylist
|
0

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

JsonParser

Comments

0

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
}

3 Comments

OP already has an array of object. What is obj1? What is use of adding JSON objects to collection without parsing?
i have only array of items no object JSONObject jObj = new JSONObject(json); its no need right
If so, how to use it? I'm always pass it as arrays, ok. You can try, or pass it as an array

Your Answer

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