0

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
1
  • FYI you always can convert a JSONObject or JSONArray to GSON
    – Kunu
    Sep 12, 2017 at 11:44

1 Answer 1

4

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
                }
            });         
1
  • how about .addJSONObjectBody on .post request ? when i passing data class there need JSONObject , i have convert by gson.toJson and casting to but the catch cannot be cast to Nov 23, 2021 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.