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);
}
Try this,
JSONArray jsonary=json.getJSONArray("invitefriend");
ArrayList<String> msgs;
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));
msgs = new ArrayList<String>(map.values());
System.out.println("MESSAGE : " + msgs);
// adding HashList to ArrayList
data.add(map);
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(),msgs,Toast.LENGTH_LONG).show();
}
});
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()
.
You have to do ""+array list Then it will display all array list items in toast. Or if you need all one by one,then take a for loop with length of array list in that u just add array list[i]like
for (int i=0;i<=arraylist.length;i++){
Toast.makeText(getContext(),arraylist[i],Toast.LENGTH_SHORT).show();
}
Try like this. i think its helpfull
featured_list = new ArrayList<Featured>();
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Toast.makeText(getApplicationContext(), object.getString("thumb").toString(), Toast.LENGTH_LONG).show();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.