Android Volley With Examples

In this tutorial, we’ll be discussing at length, an important networking library in our application, namely Android Volley. Android Volley library has been developed by Google. The first glimpses of it were seen in Google I/O . Let’s see what it has in store!

Android Volley

Networking is a key component in most of the Android Applications today. Using an AsyncTask might not be the best option when we have too many networking requests to handle. AsyncTask can’t prioritize parallel/multiple requests in an orderly manner. Besides they have a lot of boilerplate code. Here’s where Android Volley library comes as a handy tool. It has some powerful features and is a lot faster than AsyncTask.

Advantages of Android Volley Library

  1. Automatic scheduling of all network requests.
  2. Multiple concurrent network connections.
  3. Requests queuing and prioritization.
  4. Caching responses. It supports memory and disk-based caches.
  5. Cancelling one or more requests.
  6. Built-in support for String, Images, JSONObject and JSONArray Requests.
  7. Ability to create custom requests.
  8. Built-in NetworkImageView widget to load images from a URL easily.
  9. Support for retrying requests.
  10. Powerful debugging and tracing tools to catch errors.

Getting Started with Android Volley

To integrate Volley in your Android Studio Project, you need to add the following dependency in your build.gradle file:

Unlike Retrofit, which requires a bunch of libraries like OkHttp, Picasso for complete functionalities, Volley Library is much smaller in size.

Note: With the introduction of Android Studio 3.0 IDE the keyword compile is replaced by implementation in the dependencies. Both can be used interchangeably.

The Fundamental classes of Volley are listed below.

  • RequestQueue
  • Requests
  • Response

Android Volley RequestQueue

This is the basic building block of Volley. The name speaks for itself. A RequestQueue is used to queue all the requests and handle the responses. Also, it is responsible for managing the worker threads for parallel requests besides reading and writing to and from the cache. A RequestQueue works in a FIFO manner(First In First Out). At a time it can handle four requests simultaneously. A RequestQueue is instantiated in the following way in a class.

this refers to the context. It can be activity or application’s context. The above code initializes the RequestQueue with a default set of parameters. We can initialize the RequestQueue with our own set of parameters as shown below.

We’ve initialized our own instances of Cache and Network with custom parameters.
Call the method mRequestQueue.stop() to stop the queue that’ll handle the requests.

Android Volley Request

Request instances are built to define a network request. These are used to support GET and POST requests.
Request class acts as the base class which can be extended to define a custom request.
Volley provides the following types of Requests built-in:

  • StringRequest
  • JsonObjectRequest
  • JsonArrayRequest
  • ImageRequest

Android Volley StringRequest

StringRequest is used when you want the response returned in the form of a String. You can then parse the response using Gson or JSON as per your requirement.
StringRequest has two forms of constructors as shown in the image below.

android-volley-stringrequest-constructor

We’ll discuss the first constructor here.

  • method : Expects an argument among GET, POST, PUT, DELETE.
  • url : URl to fetch the response at.
  • listener : Listens for a success response. We need to implement and override the following method in here.
  • errorListener: Listens for an error response. We need to implement the following method in here.

A sample code snippet of a string request is given below.

The response or error will be delivered to the onResponse/onErrorResponse classes that we defined in our request above.
queue.add(stringRequest); is used to add the Request to the RequestQueue.
The code snippet written above can be simplified to improve the readability.

The constructor looks much better now.
To send parameters in request body we need to override either the getParams() or getBody() method of the request class.
Additionally, we can override the getHeaders() and the getBodyContentType() methods too to specify the headers and content type formats respectively.
getHeaders() are useful for authentication.
To override the Request Method type from the first parameter, getMethod() is implemented.
The constructor with skeleton code of the above mentioned methods is given below.

Parameters and headers are specified as a key-value pair in the getParams() and getHeaders() methods respectively.

Note: getParams() method is ignored when the request type is a GET. So to pass parameters we’ll need to concatenate them in the URL string as shown below.

%1$s is used for the first param. %2$s, %3$s and so on for the second, third parameters.

Android Volley JsonObjectRequest

JsonObjectRequest is used to send and receive JSONObject from the server. It extends the class JsonRequest.
The two forms of constructors are given below.
android-volley-stringrequest-constructor
We’ll look at the second constructor here.

An optional JSONObject can be passed as the request body in the second parameter of the constructor.
We’ve set it to null for now.

JsonObjectRequest can override the same set of methods (getHeaders(), getBodyContentType(), getBody(), getMethod()) like the StringRequest except getParams().

JsonObjectRequest is meant for passing a JSONObject with the request. Hence getParams() gets ignored.
Overriding getBody() isn’t neccessary in a JsonObjectRequest since passing a JSONObject as the second parameter does the same implicitly.

If you eventually override getBody(), the second parameter passed in the constructor would be ignored.
Since the Request type isn’t defined in the above constructor, the request would automatically identify the method(GET,POST, PUT or DELETE) from the url. To explicitily specify the method type we can override the getMethod()

Android Volley JsonArrayRequest

JsonArrayRequest is used to send and retrieve JSONArray to and from the server. It extends the JsonRequest class and functions the same way as JsonObjectRequest class.
A JSONArrayRequest can’t handle a request that returns a JSONObject.
Following are the two forms of constructors.

Let’s look at the second constructor.

Android Volley ImageRequest

Using ImageRequest is the classical and standard way to fetch images from a URL. An ImageRequest returns a Bitmap Object that we can eventually display in our ImageView.
Following are the two constructors present in the class.

android-volley-imagerequest

Let’s use the second constructor since the first one now stands deprecated.
Besides the url, response and error listeners, the constructor expects the width, height, scaleType and Config values for the bitmap.

The RequestQueue has a RequestFinishedListener. The listener gets triggered everytime a request in the queue is finished.

Android Volley Error Handling

onErrorResponse() passes an instance of VolleyError as we have seen above.
Following are the major types of errors that can be returned in the instance.

  • AuthFailureError: When the authentication fails.
  • NetworkError: Server Error, DNS issues etc.
  • NoConnectionError: When there is no internet connection.
  • ParseError: Generally returned in a JsonObjectRequest or JsonArrayRequest when the JSON response is malformed.
  • ServerError: The server responded with an error status code (401, 500 etc)
  • TimeoutError: When a request timeout occurs. Volley has a default timeout of 2.5 seconds.

Android Volley Cancelling Requests

Volley has a powerful API for canceling requests. Request cancellation is important in situations where the Activity needs to be destroyed. Cancellation of requests is possible using tags.

Android Volley Prioritizing Requests

A priority can be set on each request present in the RequestQueue. Doing so the RequestQueue would run the requests based on the priorities instead of FIFO. A priority can be assigned by overriding the method getPriority() in the constructor. Following are the possible states:

  • Priority.LOW: Used to load images.
  • Priority.NORMAL: Default value.
  • Priority.HIGH: Used to load texts.
  • Priority.IMMEDIATE: Used in places like login/logout.

Following code snippet assigns priority to requests.

Android Volley Caching

Caches are enabled by default for the above mentioned Request types. The response data is typically stored in the form of byte array

Disabling Cache

Accessing the Cache

Clearing cache

Invalidate cache: Invalidating a cache would show the cached data until the new response is returned. That response would override the current cache.

Android Volley ImageLoader and NetworkImageView

We’ve used an ImageRequest to retrieve an image bitmap from the response before.
Volley provides us with NetworkImageView widget that directly loads the image from the URL. Besides, it lets us set a default placeholder as well as an error drawable.
ImageLoader is responsible for loading the images using a BitmapLruCache().

setImageFromUrl() method directly sets the image returned from the url onto the NetworkImageView instance. We need to pass the URL and ImageLoader instance in this method to fetch the image.

Note: To troubleshoot and log debug traces set the following line in your code.

We can create our own Custom Requests too by extending Request<T>. The two abstract methods parseNetworkResponse() and deliverResponse() need to be overriden.
This brings an end to this comprehensive tutorial on Volley. We’ll implement Volley in an application using the above-learned concepts in our next tutorial.

Reference: GitHub Page, API Doc

By admin

Leave a Reply