victory1908
11/30/2015 - 2:28 AM

Example of fetching an Array JSON feed with volley (http://blog.chrisblunt.com/android-consuming-a-remote-json-api-with-volley/#comment-1941

class MainActivity extends Activity {
    // mEntries in this case is just an ArrayList store
    private ArrayList<String> mEntries;

    // ...
    
    // Fetch method
    private void fetch(RequestQueue requestQueue) {
        JsonArrayRequest request = new JsonArrayRequest("http://example.com/feed.json",
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray jsonArray) {
                        for(int i = 0; i < jsonArray.length(); i++) {
                            try {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                mEntries.add(jsonObject.toString());
                            }
                            catch(JSONException e) {
                                mEntries.add("Error: " + e.getLocalizedMessage());
                            }
                        }
    
                        allDone();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(MainActivity.this, "Unable to fetch data: " + volleyError.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    
        mEntries = new ArrayList<>();
        requestQueue.add(request);
    }
}
[
    {
        "name": "Entry 1",
        "code": 1001
    },
    {
        "name": "Entry 2",
        "code": 1002
    },
    {
        "name": "Entry 3",
        "code": 1003
    }
]