Android Volley Library

Sithara Rao
4 min readOct 26, 2019

This post will give you an overview of Android Volley library that helps the developers to perform tireless network operation.

What is Volley??? 🙄

Volley is a networking library introduced in Google I/O 2013 that helps us to fire an API, get the response, cache it and retry in case of error.

Note that this is suitable for fetching small files, images, JSON responses etc.,

Why the name Volley? 🤔

The dictionary meaning of Volley is “discharge in quick succession”. As the library is capable of handling the “discharging of multiple network requests and responses”, so the name Volley.

Why Volley library?

Before the introduction of Volley library, the developer had to write a piece of “boilerplate code”. So what’s that? What changes were introduced which made users migrate to volley?

  1. As the network call is a heavy duty operation, we need to call it from worker thread. *Boom* 🥴 create an Async Task
  2. Get the response from server, 😵cache it
  3. Retry while error occurs, 😨 write a proper logical code for retry mechanism
  4. Scroll to next page, 😰 cancel the existing requests

By the time these things are done, mind would have reached a saturation state🤯

So, how about a library dealing with all these stuffs? You just need to call the API and everything is done! Yes!!! Volley does all these things for you

Let us now know the architecture of Volley library.

Volley Architecture

Volley architecture
  1. The requests that are placed by the app in UI thread [main thread] are put in a Request queue and are dispatched by a Request dispatcher.
  2. The control is next given to Cache thread [worker thread]. This searches the cache if there exists any data stored. If yes, the response is given to Main thread. Else the control is given to Network thread
  3. The network dispatcher asks the server from Network thread [worker thread] for data if there is a cache miss. The response obtained is given to Main thread.

“Please note that irrespective of placing the requests in Main/Background thread, volley always gives us the response on Main thread”

Add the dependency

Before starting with the coding part, we need to add the volley dependency in our gradle file

implementation 'com.android.volley:volley:1.1.1'

Also, don’t forget to add the internet permission in manifest

<uses-permission android:name="android.permission.INTERNET"/>

Creating Requests and adding it to RequestQueue

So there are two main classes Request and RequestQueue.

  1. Request class demands for few parameters based on the type of request the app is calling. Let me take an example of JsonObjectRequest

[Note that there are other inbuilt requests such as StringRequest, ImageRequest etc.,]

Json Object request

The major parameters for forming a JsonObjectRequest are

a) URL

b) JSONBody: required if any JSON object needs to be sent

c) SuccessListener: The interface where we receive onSuccess() callback when proper response is obtained

d) ErrorListener: The interface which gives us the onError() callback when there exists an error [Server error/ Network error etc.,]

Specifying the Request Method [GET/POST] is optional for few inbuilt requests.

2. As the name suggests, the requests are queued up in the Request Queue based on priority [NR → Network Request]

Request Queue

Code snippet

I hope you all have got a rough idea on placing the request. Let’s see the snippet for understanding JsonObjectRequest:

As explained earlier, placing the request is done in 2 steps:

  1. Creating the request
  2. Adding the request to the Request Queue

Here, Custom Volley is a singleton class with add, cancel methods.

The object creation of request queue class needs app context parameter

requestQueue = Volley.newRequestQueue(VolleyDemoApplication.getAppContext());

On calling addToRequestQueue(JsonObjectRequest request, String tagName) method, the requestQueue reference adds the request to its queue.

On successfully fetching the response, onSuccess() callback is received and on error scenarios, we receive onError() callback.

Finding if the response is cached

Before adding the request into the queue, we can check if the response for the given URL is null or not by calling below method. We can even find out ttl [time to live] parameter etc.,

requestQueue.getCache().get(url)

Default Retry policy

Volley offers default retry mechanism to place the request again, in case of few error scenarios. We can set the setRetryPolicy() method on the request.

The Default retry policy takes 3 parameters:

  1. initialTimeOutMs : The initial timeout for the policy
  2. maxNumRetries: The maximum number of retries to be performed
  3. backOffMultiplier: Backoff multiplier for the policy

Working:

Let’s assume,

  1. initialTimeOutMs = 5s
  2. maxNumRetries = 2
  3. backOffMultiplier =2

The retry calculation happens in the following way,

initialTimeOutMs = initialTimeOutMs +(initialTimeOutMs * backOffMultiplier)

Retry 1 → 5 +(5*2) = 15s (Request dispatched with timeout of 15s)

Retry2 → 15 +(15*2) = 45s (Request dispatched with timeout of 45s)

Since the max retries are 2, the retry will stop after 2 trials. After 2 trials if success response is not obtained, then there will be Timeout Error.

So this is a small introduction to Volley library. In the further posts, I will be briefing on customizing the requests.

--

--