1

I am trying to get the jsonobject into next screen listview. I can get 1 values in listview but i have multiple values to be get fetched. How can I do.

here is my code for getting string from server :-

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("Get_Friend_List", holder
                .toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        StringEntity se = new StringEntity(holder.toString());
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));
        httppost.setEntity(se);
        HttpResponse response = httpclient.execute(httppost);
        resp = response.toString();
        String t = EntityUtils.toString(response.getEntity());

        try {
            JSONArray get_string1 = new JSONArray(t);
            JSONObject get_string = null;

            // Receive the JSON object from server
            String userid = (String) get_string.get("UserId");
            String totalusers = (String) get_string.get("TotalUsers");
            String SessionID = (String) get_string.get("SessionID");
            for (int i = 0; i < get_string1.length(); i++) {
                get_string = get_string1.getJSONObject(i);
                JSONObject contact = (JSONObject) get_string
                        .get("contacts-" + i);

                String contact_id = (String) contact.get("UserId");

                contact_username = (String) contact.get("UserName");
                contact_status = (String) contact.get("Status");
                Contacts.setStatus(contact_status, i);
                Contacts.setUserName(contact_username, i);
            }

        } catch (JSONException e) {
            e.printStackTrace();
            System.out.println("--error--" + e.getMessage());
        }

    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }  
}

here is the response from server which I am getting. but storing only last value in listview.

 Response from Server----{
"UserId":   "1",
"TotalUsers":   "4",
"contacts-0":   {
        "UserId":   "3",
        "UserName": "kumar",
        "Status":   "1"
},
"contacts-1":   {
    "UserId":   "2",
    "UserName": "rahul",
    "Status":   "1"
},
"contacts-2":   {
"UserId":   "4",
"UserName": "vlk",
    "Status":   "1"
},
"contacts-3":   {
    "UserId":   "5",
    "UserName": "vlk",
    "Status":   "1"
},
"SessionID":    "39355"
}

contacts.java

public class Contacts {
public static String[] status = new String[100];

public static String[] usrname = new String[100];

public static String getStatus(int i) {
    return status[i];
}

public static String getUserName(int i) {
    return usrname[i];
}

public static void setStatus(String status, int i) {
    Contacts.status[i] = status;
}

public static void setUserName(String username, int i) {
    Contacts.usrname[i] = username;
}

}
5
  • 1
    Please include the json string and code that populate json objects/arrays in your post. Commented Jul 2, 2012 at 8:00
  • @AVD updated with source code.. please can you say me, how to fetch multiple data from server. Commented Jul 2, 2012 at 9:06
  • What is `Contacts'? Is it List<T>? How many times the loop is repeated? Commented Jul 2, 2012 at 9:11
  • its an class where I am setting getter & setter function to stored values.. Commented Jul 2, 2012 at 9:16
  • updatede with contacts.java class Commented Jul 2, 2012 at 9:17

1 Answer 1

1

The json String has no array type. You need to call the has() method (to test a json attribute is present or not) before you read contacts object. Another issue is in Contacts type. You should have to define the Contact type and initialize List<Contact> to store one or more contacts.

Have a look at the Contact class

public class Contact
{
    private String userId;
    private String userName;
    private String status;

    public Contact() {
         userId="";
         userName="";
         status="";
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "Contact{" + "userId=" + userId + ", userName=" + userName + ", status=" + status + '}';
    }

}

And code to read/parse json string.

 JSONObject get_string = new JSONObject(t);

 String userid = (String) get_string.get("UserId");
 String totalusers = (String) get_string.get("TotalUsers");
 String SessionID = (String) get_string.get("SessionID");

 int i=0;
 JSONObject obj=null;

 /* Initialize the List<Contact> */
 List<Contact> list=new ArrayList();

 while(get_string.has("contacts-"+i))
  {
   obj=get_string.getJSONObject("contacts-" + i);
   Contact contact=new Contact();
   contact.setUserId(obj.getString("UserId"));
   contact.setUserName(obj.getString("UserName"));
   contact.setStatus(obj.getString("Status"));
   list.add(contact);
   i++;
  }
Sign up to request clarification or add additional context in comments.

Comments

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.