how to parse android volley gson request to a list / collection of objects
Asked Answered
P

2

4

I am trying to make a Gson request using android volley. Currently it it working correctly and I am very happy. However why I try and get a List<> or a collection of objects my code no longer works.

Current code:

public class ReviewModel 
{
  public long Id;
  public Strring Description;
}

here is how I use my gson class:

    GsonRequest<ReviewModel> jsObjRequest = new GsonRequest<ReviewModel>(Request.Method.GET,
            url, ReviewModel.class, new Response.Listener<ReviewModel>() {
                @Override
                public void onResponse(ReviewModel response) {
                    ReviewsHandleOkResponse(response);
                }

            }, new ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    ReviewsHandleErrorResponse(error);
                }
            });
    Network.getInstance(getActivity()).addToRequestQueue(jsObjRequest);
}

here is my Volley GSON Request Class:

public class GsonRequest<T> extends Request<T> {
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Map<String, String> params;
private final Listener<T> listener;

/**
 * Make a GET request and return a parsed object from JSON.
 * 
 * @param url
 *            URL of the request to make
 * @param clazz
 *            Relevant class object, for Gson's reflection
 * @param headers
 *            Map of request headers
 */
public GsonRequest(int method, String url, Class<T> clazz,
        Map<String, String> headers, Map<String, String> params,
        Listener<T> listener, ErrorListener errorListener) {
    super(method, Network.getFullUrl(url), errorListener);
    this.clazz = clazz;
    this.headers = headers;
    this.params = params;
    this.listener = listener;
}

/**
 * Recieves header
 * 
 * @param method
 * @param url
 * @param clazz
 * @param params
 * @param listener
 * @param errorListener
 */
public GsonRequest(int method, String url, Class<T> clazz,
        Map<String, String> params, Listener<T> listener,
        ErrorListener errorListener) {
    super(method, Network.getFullUrl(url), errorListener);
    this.clazz = clazz;
    this.headers = new HashMap<String, String>();
    this.params = params;
    this.listener = listener;
}

/**
 * No params or headers
 * 
 * @param method
 * @param url
 * @param clazz
 * @param listener
 * @param errorListener
 */
public GsonRequest(int method, String url, Class<T> clazz,
        Listener<T> listener, ErrorListener errorListener) {
    super(method, Network.getFullUrl(url), errorListener);
    this.clazz = clazz;
    this.headers = new HashMap<String, String>();
    this.params = new HashMap<String, String>();
    this.listener = listener;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    return headers != null ? headers : super.getHeaders();
}

@Override
public Map<String, String> getParams() throws AuthFailureError {
    return params != null ? params : super.getParams();
}

    @Override
protected void deliverResponse(T response) {
    listener.onResponse(response);
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        String json = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        return Response.success(gson.fromJson(json, clazz),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
    }
  }
}

however when I rey and parse it to a List<ReviewModel> I get compile errors.

From what I have researched, I need to do some thing like :

Type collectionType = new TypeToken<ArrayList<ReviewModel>>(){}.getType();      
    // api/v1/reviews/products/{productId}/{pageNumber}/{pageSize}
    String url = "api/v1/reviews/products/" + productId + "/" + currentPage + "/" + pageSize;
    GsonRequest<ArrayList<ReviewModel>> jsObjRequest = new GsonRequest<ArrayList<ReviewModel>>(Request.Method.GET,
            url, (Class<ArrayList<ReviewModel>>) collectionType, new Response.Listener<ArrayList<ReviewModel>>() {
                @Override
                public void onResponse(ArrayList<ReviewModel> response) {
                    ReviewsHandleOkResponse(response);
                }

            }, new ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    ReviewsHandleErrorResponse(error);
                }
            });
    Network.getInstance(getActivity()).addToRequestQueue(jsObjRequest);
}

but this gives me the following error:

10-28 21:12:59.940: E/AndroidRuntime(28564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.menu/com.menu.activities.ProductViewActivity}: java.lang.ClassCastException: com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl cannot be cast to java.lang.Class

Do i need to modify my GsonRequest Class, and add another constructor which doesn't take a type class?

Pehlevi answered 28/10, 2014 at 19:13 Comment(6)
take a look at gson's TypeToken.Pageboy
I have mentioned it in my post, however I cannot figure out how it fits into my GsonRequest Class.Pehlevi
(Class<ArrayList<ReviewModel>>) collectionType don't cast. Type is not Class. Modify your GsonRequest to accept Type.Pageboy
so instead of receiving Class<T> it should just receive Type? Then what return type should my ParseNetworkResponse() be? cause Response<T> wont work then will it?Pehlevi
you T is still parametrized at the creation of the instance, so it is still valid.Pageboy
Take a look at this answer -> https://mcmap.net/q/827197/-how-to-parse-android-volley-gson-request-to-a-list-collection-of-objectsAppointment
D
3

Use YourClass[] for List of objects in Volley request. It has solved my issue. The following code should help you:

GsonRequest<ReviewModel[]> jsObjRequest = new GsonRequest<ReviewModel[]>(Request.Method.GET,
        url, ReviewModel[].class, new Response.Listener<ReviewModel[]>() {
            @Override
            public void onResponse(ReviewModel[] response) {
                ReviewsHandleOkResponse(response);
            }
        }, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ReviewsHandleErrorResponse(error);
            }
        });
Network.getInstance(getActivity()).addToRequestQueue(jsObjRequest);
}
Diehl answered 17/9, 2015 at 5:28 Comment(2)
We can also achieve this behaviour by creating a new bean lets say ParentModel, which will have list of ReviewModel as variable and just getters and setter for the same.Hence you can directly replace the ReviewModel[] with ParentModel in above seudo codeDiehl
@Zapnologica: accept this as answer if it has resolved your problmDiehl
B
2

I recently needed to implement a solution myself so here you go. Custom GsonArrayRequest.

public class GsonArrayRequest<T> extends Request<ArrayList<T>> {
     private final Gson gson = new Gson();
     private final Class<T> clazz;
     private final Map<String, String> headers;
     private final Response.Listener<ArrayList<T>> listener;

    /**
     * Make a GET request and return a parsed object from JSON.
     * @param clazz Relevant class object, for Gson's reflection
     * @param url     URL of the request to make
     * @param headers Map of request headers
     */
     public GsonArrayRequest(String url, Class<T> clazz, Map<String, String> headers,
                        Response.Listener<ArrayList<T>> listener, Response.ErrorListener errorListener) {
    super(Method.GET, url, errorListener);
    this.clazz = clazz;
    this.headers = headers;
    this.listener = listener;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    return headers != null ? headers : super.getHeaders();
}

@Override
protected Response<ArrayList<T>> parseNetworkResponse(NetworkResponse response) {
    try {
        String json = new String(
                response.data,
                HttpHeaderParser.parseCharset(response.headers));

        Type listType = com.google.gson.internal.$Gson$Types.newParameterizedTypeWithOwner(null, ArrayList.class, clazz);
        ArrayList<T> tList = gson.fromJson(json, listType);
        return Response.success(
                tList,
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
    }
}

     @Override
     protected void deliverResponse(ArrayList<T> response) {
         listener.onResponse(response);
     }
 }

You can use it like this:

GsonArrayRequest<StoreWorkout> gsonArrayRequest = new GsonArrayRequest<>(
        url, AngryPugs.class, null, new Response.Listener<ArrayList<AngryPugs>>() {
    @Override
    public void onResponse(ArrayList<AngryPugs> response) {
        // your list here
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

After adding the request

 VolleySingleton.getInstance(this).addToRequestQueue(gsonArrayRequest);
Blake answered 18/3, 2017 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.