I am using json parsing,i am sending five emails and five names and in response it gives
{
"invitefriend": [
{
"message": "Friend Request sent to : abc"
},
{
"message": "Friend Request sent to : def"
}
]
}
now what i am trying is i want to get abc and def in toast,but it gives one by one seperatly
JSONArray jsonary=json.getJSONArray("invitefriend");
for (int i = 0; i < jsonary.length(); i++) {
JSONObject c = jsonary.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
// map.put(INTERESTACCEPT_USER_ID, c.getString(INTERESTACCEPT_USER_ID));
map.put(TAG_SUCCESS_INVITE,c.getString(TAG_SUCCESS_INVITE));
map.put(TAG_SUCCESS_MESSAGE, c.getString(TAG_SUCCESS_MESSAGE));
ArrayList<String> msgs=new ArrayList<String>();
msgs=map.put(TAG_SUCCESS_MESSAGE, c.getString(TAG_SUCCESS_MESSAGE));
System.out.println("MESSAGE : " + msg);
// adding HashList to ArrayList
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(),""+msgs, Toast.LENGTH_LONG).show();
}
});
data.add(map);
}
You're getting multiple Toast
s because you're Toast
ing inside the for
loop. Simply move that call to outside the loop:
JSONArray jsonary=json.getJSONArray("invitefriend");
ArrayList<String> msgs=new ArrayList<String>();
String msg = null;
for (int i = 0; i < jsonary.length(); i++) {
JSONObject c = jsonary.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
// map.put(INTERESTACCEPT_USER_ID, c.getString(INTERESTACCEPT_USER_ID));
map.put(TAG_SUCCESS_INVITE,c.getString(TAG_SUCCESS_INVITE));
map.put(TAG_SUCCESS_MESSAGE, c.getString(TAG_SUCCESS_MESSAGE));
msg=map.put(TAG_SUCCESS_MESSAGE, c.getString(TAG_SUCCESS_MESSAGE));
msgs.add(msg);
System.out.println("MESSAGE : " + msg);
// adding HashList to ArrayList
data.add(map);
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), msgs.toString(), Toast.LENGTH_LONG).show();
}
});
Also, we've defined the ArrayList
msgs
to collect the response messages, which we Toast
directly with msgs.toString()
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments