Skip to content

Instantly share code, notes, and snippets.

@richardleandro1
Forked from cblunt/MainActivity.java
Created June 10, 2017 16:52
Show Gist options
  • Save richardleandro1/e9fb2740113d9c54b5c0957c3162623f to your computer and use it in GitHub Desktop.
Save richardleandro1/e9fb2740113d9c54b5c0957c3162623f 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