Volley is a library developed by Google to make networking calls for Android easier.

Volley comes out with support for raw strings, images and JSON. By providing built in support for the features you need, volley allows you to focus more on the logic that is required to the app.
Volley is developed in ASOP repository at frameworks/volley.

Benefits of Volley: –

  • Automatic Scheduling of the requests.
  • Having a solution where you can prioritize your request.
  • It has the ability to cache the response.
  • Cancellation of the single request or multiple requests.

How it works: –


Volley has pool of threads, when you add the request to the queue, the cache thread picks it up, if the request is serviced from the cache, the cached response is parsed on the thread and parsed response is delivered to the main thread.

In case we got a miss and if the request cannot service by the cache thread, then it is picked up by the network thread.

The available network threads which runs in round robin pattern takes the request from the queue and performs the HTTP transaction, parse the responses on the worker thread then write the response to the cache, and posts the parsed response to the main thread for delivery.
We can request for response from any thread, but responses are always delivered on the main thread.

Volley is not for: –

Volley has in built feature where it caches the response during parsing.
Therefore, it is not suitable for large download or streaming operations.

In order to do large downloading or perform streaming operations we can use an alternative like DownloadManager.

How to set up: –

Easiest way of setting up is to clone the volley and import the project into your code.

    • Git Clone the repository by typing the following at the command line:
git clone https://android.googlesource.com/platform/frameworks/volley
  • In Android Studio, with project open, select File -> New Module and choose Import Existing Project. Select the directory where we have just cloned the volley project.
  • After Selecting, Android Studio, automatically updates your settings.gradle file to include the volley module so we just need to add our dependencies compile project (‘:volley’ ) and we are done.

Add the INTERNET Permission

In order to use volley we need to add Internet permission in our project manifest file. Without this we wont be able to connect to the network.

Sending a Simple Request

At the higher level, we use volley by creating a RequestQueue and passing it Request objects.
RequestQueue manages the task of handling the worker thread.

Requests do parsing of raw response and volley takes care of dispatching the parsed response back to the main thread.

We use add method to add the request objects to the RequestQueue.

Here is a snapshot of the code for better understanding.

final TextView mTextView = (TextView) findViewById(R.id.text);
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener() {
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
}, new Response.ErrorListener() {
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
// Add the request to the RequestQueue.

Setting up a RequestQueue

Above we discussed how to make simple request via RequestQueue taking advantage of Volley’s default behaviors.

This Section describes how to make RequestQueue as a Singleton, which makes it last the lifetime of your app.

Why Singleton Pattern

If your application makes constant use of the network, it’s probably most efficient to set up a single instance of RequestQueue that will last the lifetime of your app.

There are two ways to achieve a single instance of the RequestQueue.

  • Make a Singleton Class that encapsulates RequestQueue.
  • Second approach, is to extend the Application class and set up the RequestQueue in Application.onCreate().

Second Approach is discouraged, as mentioned in the Developer site.

Volley Over AsyncTask

We were using AsyncTask Frequently in our app code for making network connection. So, what volley framework brings to us: –

  • Volley can do multiple requests without the overhead of thread management.
  • Volley Retry is a great feature.
  • Better handling of canceling the Request.

One comment on “Android Volley Introduction

Leave a Reply

Your email address will not be published.