Issue
I'm using https://github.com/amitshekhariitbhu/Fast-Android-Networking in my project.
Does it support GSON?
if yes how does it work with it? I tried searching it in the documentation but it only contains information about JacksonParserFactory.
I found a GsonParserFactory in it Using it like below
AndroidNetworking.setParserFactory(new GsonParserFactory());
but not sure how it works as I always get
org.json.JSONObject
Solution
You can use GSON to parse the JSON response to your Java Class Object
private Gson gson;
GsonBuilder gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
//If your response id JSON array
AndroidNetworking.get("your_url")
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
@Override
public void onResponse(JSONArray response) {
//For JSON array Response
List<YourModel> responseArray = Arrays.asList(gson.fromJson(response, YourModel[].class));
}
@Override
public void onError(ANError error) {
// handle error
}
});
//If your response is JSON object
AndroidNetworking.post("your_url")
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
//For JSON object response
YourModel responseObject = gson.fromJson(response, YourModel.class);
}
@Override
public void onError(ANError error) {
// handle error
}
});
Answered By - Shriyansh Gautam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.