Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are many things you need to consider...</p> <p>1) You are accessing <strong>"message"</strong> and <strong>"sender"</strong> form the json, and interistingly, your json doesn't have any keys even similar to them...So first, create a reasonable json...probabaly JsonArray, with JsonObjects having sender and message. That might look like this...</p> <pre><code>[ {"message":"some text...","sender":"123"}, {"message":"some text...","sender":"456"}, {"message":"some text...","sender":"789"} ] </code></pre> <p>This can be easily generated form objects using <a href="http://json.codeplex.com/" rel="nofollow">NewtonsJson</a> like this.</p> <pre><code>List&lt;Message&gt; list = new List&lt;Message&gt;(); list.Add(new Message() { sender = "123", message = "some text..." }); list.Add(new Message() { sender = "456", message = "some text..." }); list.Add(new Message() { sender = "789", message = "some text..." }); string json = JsonConvert.SerializeObject(list); Console.WriteLine(json); </code></pre> <p>Where 'Message' is the simple class holding information...</p> <pre><code>class Message { public string message { get; set; } public string sender { get; set; } } </code></pre> <p>2) Now Create a similar class in you Android project for 'Message' and you can easily parse this like</p> <pre><code>private List&lt;Message&gt; decodeJson(String fromServer) { List&lt;Message&gt; list = new ArrayList&lt;Message&gt;(); try { JSONArray jsAry = new JSONArray(fromServer); for (int i = 0; i &lt; jsAry.length(); i++) { JSONObject jsObj = (JSONObject) jsAry.get(i); Message msg = new Message(); msg.setSender(jsObj.getString("sender")); msg.setMessage(jsObj.getString("message")); list.add(msg); } } catch (JSONException e) { e.printStackTrace(); } return list; } </code></pre> <p>And now, you can do any thing you want with this list of Message. You can also add more fields in you class like 'receiver' etc.</p> <p>Hope this helps...:)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload