Skip to content

Instantly share code, notes, and snippets.

@cblunt
Last active December 11, 2018 05:48
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cblunt/162beb7ecfafa1bd2ad9 to your computer and use it in GitHub Desktop.
[
{
"name": "Entry 1",
"code": 1001
},
{
"name": "Entry 2",
"code": 1002
},
{
"name": "Entry 3",
"code": 1003
}
]
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