If I have a JSON like below:
{
"Division": [
{
"DivisionId": 1,
"DivisionName" : "A"
},
{
"DivisionId": 2,
"DivisionName" : "B"
}
],
"Title": [
{
"TitleId": 11,
"Title": "Title 1"
},
{
"TitleId": 12,
"Title": "Title 2"
}
]
}
How can I get the Division
only with its values inside? What I'm trying to achieve is to put the values of Division
inside my ArrayList
. I'm using Volley to get the JSON result and what I tried is on the onResponse
I used JSONArray divisionArr = response.getJSONArray("Division");
and loop it here's my code
JSONArray divisionArr = response.getJSONArray("Division");
for (int i = 0; i < divisionArr.length(); i++) {
Division division = new Division();
JSONObject divisionObj = (JSONObject) divisionArr.get(i);
division.setId(divisionObj.getInt("DivisionId"));
division.setName(divisionObj.getString("DivisionName"));
divisionArrayList.add(division);
}
But I'm having an error ParseError
, I maybe doing it wrong, but I don't know what is it. Please help, thank you.
///////
Here's my Volley request
public void getData(Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
try{
String syncCall = Constants.VOLLEY;
request = new JsonObjectRequest(Method.GET,
syncCall,
null,
listener,
errorListener);
request.setRetryPolicy(
new DefaultRetryPolicy(
60000,//DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, // 2500
1,//DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 1
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); //1f
mRequestQueue.add(request);
} catch (Exception e) {
e.printStackTrace();
}
}
Then in my Activity
private void callSyncVolley() {
final ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Fetching data....");
pd.show();
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray divisionArr = response.getJSONArray("Division");
for (int i = 0; i < divisionArr.length(); i++) {
Division division = new Division();
JSONObject divisionObj = (JSONObject) divisionArr.get(i);
division.setId(divisionObj.getInt("DivisionId"));
division.setName(divisionObj.getString("DivisionName"));
divisionArrayList.add(division);
}
pd.dismiss();
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, "Error: " + e.getMessage());
pd.dismiss();
}
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error.networkResponse != null) {
Log.d(TAG, "Error Response code: " + error.networkResponse.statusCode);
pd.dismiss();
}
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
Log.d(TAG, "Error Response code: Timeout/NoConnection");
pd.dismiss();
} else if (error instanceof AuthFailureError) {
//TODO
Log.d(TAG, "Error Response code: AuthFailureError");
pd.dismiss();
} else if (error instanceof ServerError) {
//TODO
Log.d(TAG, "Error Response code: ServerError");
pd.dismiss();
} else if (error instanceof NetworkError) {
//TODO
Log.d(TAG, "Error Response code: NetworkError");
pd.dismiss();
} else if (error instanceof ParseError) {
//TODO
Log.d(TAG, "Error Response code: ParseError");
pd.dismiss();
}
}
};
VolleyRequestManager.getInstance().doRequest().getData(listener, errorListener);
}
The error only shows Error Response code: ParseError
See Question&Answers more detail:
os