Skip to content

Instantly share code, notes, and snippets.

@TiagoGouvea
Created July 29, 2015 11:08
Show Gist options
  • Save TiagoGouvea/10a7156d69153b75e7ef to your computer and use it in GitHub Desktop.
Save TiagoGouvea/10a7156d69153b75e7ef to your computer and use it in GitHub Desktop.
Sample request returning json and materializing it to objects, the looong way
public static void get(Context context, int idStatus, final int id, final SuccessListener successListener, final ErrorListener errorListener) {
String url = "some_url_to_get_json";
ARequest.get(context, url,
new AResponse.SuccessListener() {
@Override
public void onResponse(Object response) {
JSONArray jsonArray = null;
try {
JSONObject jsonObject = new JSONObject(response.toString());
if (jsonObject.getJSONObject("meta").getInt("code") == 200) {
if (!jsonObject.isNull("data")) {
jsonArray = new JSONArray(jsonObject.getString("data"));
ArrayList<Item> tmpItems = Items.fromJson(jsonArray);
addRange(tmpItems);
successListener.onResponse(tmpItems);
} else {
successListener.onResponse(new ArrayList<Item>());
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
errorListener.onResponse(e);
}
}
}, new AResponse.ErrorListener() {
@Override
public void onResponse(Exception exception) {
errorListener.onResponse(new Exception(exception.getMessage()));
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment