Created
July 29, 2015 11:08
-
-
Save TiagoGouvea/10a7156d69153b75e7ef to your computer and use it in GitHub Desktop.
Sample request returning json and materializing it to objects, the looong way
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
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