Skip to content

Instantly share code, notes, and snippets.

@ThomasStuetz
Created May 16, 2017 08:44
Show Gist options
  • Save ThomasStuetz/8d93102049ed5bcca5fe81d837d89397 to your computer and use it in GitHub Desktop.
Save ThomasStuetz/8d93102049ed5bcca5fe81d837d89397 to your computer and use it in GitHub Desktop.
REST-Client mit Volley
private void downloadDummyData() {
final String URL = "http://vm15.htl-leonding.ac.at:8080/DummyDataGenerator/rs/person/20";
JsonArrayRequest jsonRequest = new JsonArrayRequest(
URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
//MainActivity.personList = parseJsonArrayPersons(response);
try {
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response.get(i);
int id = MainActivity.personList.size() + 1;
String firstName = person.getString("firstName");
String lastName = person.getString("lastName");
int zipcode = person.getInt("zipCode");
String city = person.getString("city");
MainActivity.personList.add(new Person(id, firstName, lastName, zipcode, city));
personAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(),
"Error: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getActivity(),
error.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(getActivity()).add(jsonRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment