Implementing a Custom Request Using Volley Library

DZone 's Guide to

Implementing a Custom Request Using Volley Library

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Get an intro to this library here.

· Mobile Zone ·
Free Resource

What Exactly Volley Library Is:

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster.

If your response is a string, image, or JSON, you probably won't need to implement a custom Request.

Things You Need to Do:

  • Extend the Request<T> class, where <T> represents the type of parsed response the request expects. So if your parsed response is a string, for example, create your custom request by extending Request<String>.
  • Add Gson library compile dependency to your app level build.gradle
  • Create model class as per response.
  • Add custom request to request queue of volley.


This example uses the Gson library.

What Does Gson Do?

Gson is a library for converting Java objects to and from JSON using reflection.

GsonRequest class of Volley:

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 Response.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(String url, Class<T> clazz, Map<String, String> headers,
                       Response.Listener<T> listener, Response.ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.clazz = clazz;
        this.headers = headers;
        this.listener = listener;
    public Map<String, String> getHeaders() throws AuthFailureError {
        return headers != null ? headers : super.getHeaders();
    protected void deliverResponse(T response) {
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(
            return Response.success(
                    gson.fromJson(json, clazz),
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException e) {
            return Response.error(new ParseError(e));

Model class object:

  • Define Java objects that have the same names as their corresponding JSON keys, pass Gson the class object, and Gson will fill in the fields for you
public class CustomResponse {
    private String imageName;
    private String name;
    private String number;
    public String getImageName() {
        return imageName;
    public void setImageName(String imageName) {
        this.imageName = imageName;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public String getNumber() {
        return number;
    public void setNumber(String number) {
        this.number = number;

List of model class Objects:

public class CustomResponseList {
    private List<CustomResponse> customResponseList;
    public List<CustomResponse> getCustomResponseList() {
        return customResponseList;
    public void setCustomResponseList(List<CustomResponse> customResponseList) {
        this.customResponseList = customResponseList;

Main class:

  • Call GsonRequest class with specified URL and followed by model class.
  • Add Gson Request to Volley request queue.
public class CustomResponseActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        // If is the response is custom with object
    private void customResponseObject() {
        String url = "www.example.com"; // replace with your url.
        GsonRequest gsonRequest = new GsonRequest(url, CustomResponseList.class, null, new Response.Listener() {
            public void onResponse(Object response) {
                // Handle response
        }, new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {
                // Handle error
        // Add gson request to volley request queue.

Reference code: https://github.com/rajeshreddy457/Volley-android-sample

android, gson, http, java, library

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}