-
-
Save richardleandro1/e9fb2740113d9c54b5c0957c3162623f to your computer and use it in GitHub Desktop.
Example of fetching an Array JSON feed with volley (http://blog.chrisblunt.com/android-consuming-a-remote-json-api-with-volley/#comment-1941752016)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "Entry 1", | |
"code": 1001 | |
}, | |
{ | |
"name": "Entry 2", | |
"code": 1002 | |
}, | |
{ | |
"name": "Entry 3", | |
"code": 1003 | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment