Android Volley HTTP Library tutorial

Android Volley is a HTTP library for Android app developers. Previous to the Volley library, making a standard HTTP request was kind of tedious, involving both an HttpUrlConnection and (hopefully) an AsyncTask.

When using the Volley library, you don’t have to worry about implementing anything – Volley does this for you.
Volley can be seen as an abstracted layer above HttpUrlConnection, and is a great library for developers short on time. In other words, practically everyone.

Volley is developed by Google, and is available on GitHub.

How does the Volley library work?

Android volley library tutorialBefore diving into the implementation and actual method calls, let’s have a look at what the Volley library actually is. By getting a pretty good grasp of how it works behind the scenes, it is a whole lot easier to use the library as intended.
Generally, whenever you want to use any library (not limited to Android development), you should have a look at the source code – or at least the documentation.

By having a look at the source code and the documentation, it is clear that the library actually uses the native HttpUrlConnection behind the scenes. For some of you, this may be obvious.

With that said, I think it’s an important thing to mention, so programmers just starting out can get a feel for how libraries actually works. The “magic” provided by many libraries is in fact just a an abstracted implementation of the very same objects you would use if you were to do it manually!

We now know how the Volley library communicate through Http connections. But what about the UI thread?
For those unfamiliar with this term, the “UI thread” is the User Interface thread.

If we were to do network operations directly on the UI thread, the user interface would freeze until the network operation was finished. This is almost always not what you want – you want the to perform the network operations on a separate thread, or background task if you will. This way, the user can still interact with the application, while data is being downloaded in the background.

The Volley library do all this work for us, with the use of Threads. Each HTTP request is executed in a separate thread, and when the data has been downloaded, the caller get notified through an interface, or callback interface of you will.

With that said, there is a whole lot more going on behind the scenes, but that’s not something we’ll need to dive into right now. Let’s implement the Volley library!

If you're serious about Android development, I highly recommend the Google-certified Android App Development Certification Course

Use the coupon code LIMITED25 to get 25% off 😉

How to use the Volley library in Android

The first thing one should do is to set up a RequestQueue. The RequestQueue in Volley manages background (worker) threads,  and is responsible for delivering the network results back to the main thread of your application.

Note that the RequestQueue should be implemented using a Singleton Pattern. In short, this means that you’ll have only one single instance of the RequestQueue throughout the whole lifecycle of your application. If you’re unfamiliar with the Singleton Pattern, you can have a read about it here.

The RequestQueue will handle all the network operations for you. All you have to do, is to provide it with a Request. In this tutorial we’ll be working with a JsonRequest, since this is something you’ll see alot if you’re working against an API.

A JsonRequest is constructed with four parameters:

  1. The method type (POST, GET, DELETE etc.)
  2. The URL
  3. JSON-object to post to the URL. This can be null if you’re not posting anything.
  4. A Response.Listener, which is fired when the network operation is done.

Finally, when you have constructed both your RequestQueue and Request, all you have to do is call the add-method of your RequestQueue, passing in your Request.

Let’s implement some usage examples in an imaginary application. Instead of going with unrealistic examples (which alot of tutorials do!), we’ll go with a real-life example.
For the rest of this tutorial, assume that we’re developing a movie database application. The use-cases for our imaginary application is as follows:

  1. The user should be able to list popular movies (GET).
  2. The user should be able to rate movies (POST).
  3. The user should be able to delete ratings (DELETE).

The first thing we’ll do is to create a class containing the RequestQueue, built with a Singleton Pattern. We’ll call this class ApiManager.

We can now obtain an instance of the ApiManager by calling ApiManager.getInstance(this) in our activities.

Before implementing the methods to solve the actual use cases, we have one problem that needs to be addressed. How will the RequestQueue communicate with our activities?
We’ll solve this by creating an interface, which will be fired when the RequestQueue is done.  We’ll call this interface ApiListener.

It’s time to implement the three methods which will solve our use-cases.

The final ApiManager class now looks like this:

Now that we’re done with the logic, all that’s left to do is to implement this in our activities.
The following code stub shows how you would make use of the ApiManager class.


You’ve now seen a few examples on how to use the Volley-library to perform network request in your Android application.
In this tutorial we stuck with using the JsonObjectRequest. There is also possible to use both StringRequest and ImageRequest.
For further documentation, please refer to the official GitHub-repository for Volley.

Leave a Reply

Your email address will not be published. Required fields are marked *