Reputation: 561
I want to fetch countdata using JsonParsing. How is it possible? My json Response is is,
{"data":[{"l_id":"1","p_id":"5","user_id":"15"},{"l_id":"2","p_id":7","user_id":"16"}]}{"countdata":2}
countdata is Total number of data. I am able to fetch data but not able to countdata. please guide me.
I am able to fetch data for i_id,p_id and user_id Successfully and My code is,
private class GetDetail extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
dataList = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONFunctions.getJSONfromURL(urlData);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
String lId=jsonobject.getString(TAG_L_ID);
String pId=jsonobject.getString(TAG_P_ID);
String userId=jsonobject.getString(TAG_User_ID);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
But I am not able to fetch data for countdata. and give the error, java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
And my Code is,
private class getData extends AsyncTask<Void, Void, Void> {
JSONObject jsonobject;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
arr = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONFunctions.getJSONfromURL(urlCount);
String strCount = jsonobject.getString("qdata");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
Please Guide me. Thanks in advance.
Upvotes: 1
Views: 83
Reputation: 24848
First of all your json response in Invalid
Valid json :
{"data":[{"l_id":"1","p_id":"5","user_id":"15"},{"l_id":"2","p_id":"7","user_id":"16"}],"countdata":"2"}
How get countdata :
String strCount = jsonobject.getString("countdata");
Upvotes: 2