I want to add each user response to my list listItem, inserting the response's specific key and value pair. However, in this code, the first loop of getCat() executes 4 times ... then all responses get added to the last key those are 4.
public void getCat(){
String url = "http://******/*****/*****.json";
HashMap<String, List<CatType> hmap=new HashMap<String, ArrayList<CatType>>();
private List<ListItem> listItems;
for(int i=1;i<5;i++)
{
JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Response: " + response.toString());
parseJsonFeed(response);
listItems = new ArrayList<ListItem>();
listItem.addAll(O_listItems);
hmap.put("["+i+"]",listItem);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
private void parseJsonFeed(JSONObject response) {
try {
JSONArray feedArray = response.getJSONArray("food");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
OrderListItem item=new OrderListItem();
item.setId(feedObj.getInt("id"));
item.setImage(feedObj.getString("image"));
item.setItem_name(feedObj.getString("name"));
O_listItems.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
}