Reputation: 91
I got confused how to separate JsonArray
data. I want to separate the received chat messages so it appear one by one in each bubble not in becoming into one big bubble.
What am I supposed to do, to separate the data from each other?
Can anyone please help me? Thanking you in advance.
My json part of the code:
if(!content.equals("null")){
try{
JSONArray jArr = new JSONArray(content);
String messages="";
for(int i=0; i < jArr.length() ; i++){
JSONObject jObj = jArr.getJSONObject(i);
String message = jObj.getString("message");
messages += message+"\n";
}
showMessage(messages, false);
}catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
Toast.makeText(ChatRoom.this, "Error", Toast.LENGTH_LONG).show();
}
Logcat:
06-22 09:15:20.486: D/ADBUG(519): content: [{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"testing chat"},{"firstname":"teach","message":"percobaan"},{"firstname":"teach","message":"per"},{"firstname":"teach","message":"tesssssssssss"},{"firstname":"teach","message":"ddffs"},
I want my data become like this:
Upvotes: 0
Views: 1118
Reputation: 184
Change one line Like this:
String messages="";
for(int i=0; i < jArr.length() ; i++){
JSONObject jObj = jArr.getJSONObject(i);
String message = jObj.getString("message");
showMessage(messages, false);//move it to here but not outside of 'for'
}
Upvotes: 1