Reputation: 161
I am beginner to android .I am making one application related to webservice.I am getting json by hitting url and I want to show in listview .I tried so many ways but no result.I got json exceptions.please help me Here is my json:
[
[
{
"id":"9637",
"country":"Australia",
"time":"14:00",
"type":"country",
"status":"good"
},
{
"id":"9638",
"country":"india",
"time":"16:00",
"type":"country",
"status":"good"
}
]
]
code:
class Response extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
jsonResponse = EntityUtils.toString(httpEntity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
Toast.makeText(MainActivity.this,jsonResponse,Toast.LENGTH_SHORT).show();
try {
JSONArray jsonArray=new JSONArray(jsonResponse);
for(int i=0;i<jsonArray.length();i++)
{
Toast.makeText(MainActivity.this,""+jsonArray.length(),Toast.LENGTH_SHORT).show();
}
// JSONArray jsonArray=jsonObject.get
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onPostExecute(result);
}
Upvotes: 0
Views: 483
Reputation: 3313
I think this code is correct:
try {
JSONArray json=new JSONArray(jsonResponse);
JSONArray jsonArray=json.getJSONArray(0);
for(int i=0;i<jsonArray.length();i++)
{
Toast.makeText(MainActivity.this,""+jsonArray.length(),Toast.LENGTH_SHORT).show();
}
// JSONArray jsonArray=jsonObject.get
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
because your JSON Response is an array of arrays.
Upvotes: 1