Get your response using the below method:
public static String getWebserviceResponse(String p_url) {
String m_response = null;
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(p_url);
HttpResponse response;
System.err.println("Request URL---------->"+ p_url);
try {
response = client.execute(httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream in = response.getEntity().getContent();
StringBuilder sb = new StringBuilder();
String line = "";
BufferedReader bf = new BufferedReader(
new InputStreamReader(in));
while ((line = bf.readLine()) != null) {
sb.append(line);
}
m_response = sb.toString();
}
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return m_response;
}
Parse your response as below:
String response=getWebserviceResponse("your request Url");
JSONObject jo = new JSONObject(response);
JSONArray resultarr = jo.getJSONArray("msg");
for (int i = 0; i < resultarr.length(); i++)
{
JSONObject jObjresult = resultarr.getJSONObject(i);
String index = jObjresult .getString("index");
String userId=jObjresult .getString("uid"));
JSONArray jObj = jObjresult.getJSONArray("msg");
for (int j = 0; j < jObj.length(); j++)
{
//Get the messages only from the Sender C.
String sender=jObj.getString("sender");
if(sender.equalsIgnoreCase("C")
{
String message = jObj.getString("message");
}
}
}
3
solved How to parse jsonArray in android