Android Volley | Making A Synchronous Request

Photo Credit: jakedrzewiecki via Compfight cc

One of the newest frameworks in Android is Volley. This library was introduced to ease the process of making a REST web service call simpler. Usually the android volley library is used to make a request in background. For example, when a web service is called from an activity through volley library. A background worker thread is spawned, and in that thread web API call is made. After which the result is captured and returned back to the activity i.e the main UI thread. But in this tutorial I would show you how to make an Android volley synchronous request. That is, the calling thread would wait until the result is returned from the worker thread.

A point to note here is that, in any situation we should not do any sort of heavy work in the main thread. Also if you try to perform the below mentioned approach on the main thread of your app, you would get a TimeoutException. Therefore for making an android volley synchronous request I would spawn a thread and will make that thread wait until the response is captured.

Android Volley library is available through open AOSP repository. But as mentioned in this tutorial, there are two ways to include and use Volley.

Gradle Dependency

Include a gradle dependency in your project’s build.gradle (app) file:


Library Project

Steps to include the Android Volley library in your project are:

  1. Clone the repo from this url:

    It can be done through any of the leading tools like Atlassian SourceTree, TortoiseGIT or through the GIT command line.
  2. In Android Studio goto File -> Import Module -> Specify the directory
  3. Enter the module name- “:Volley” (for internal project reference), hit finish.
  4. By now the gradle would start building the project. Let it complete.
  5. Open build.gradle file for your app module
  6. In dependencies block add a line compile project(':Volley')
  7. Go to Build menu -> Rebuild Project
  8. You are done importing Android Volley to you project.

Before starting with the code don’t forget to add an internet permission in your android manifest:

Custom Volley RequestQueue

To understand the basic working of volley, please have a look at my Android Volley tutorial. When using volley I prefer to use a single RequestQueue for my application, as it is easy to keep track of requests and responses throughout the app. Hence in this Android Volley | Making A Synchronous Request tutorial as-well I have customized the volley RequestQueue, by making it a singleton class. This way only one instance of volley RequestQueue would exist. Have a look at the code below:

Although to make an Android Volley synchronous request, it is not necessary to customize the RequestQueue. As I mentioned above its just a good way of doing it. In the above class if you see closely, you would notice that cache limit for the request queue has also been increased. In standard vanilla implementation of volley, the limit of DiskBasedCache is of 5 MB, for this example I have raised to 10 MB. This would allow us to cache a larger amount of requests. You can define it accordingly, as per your requirement.

Can I do a synchronous request with volley?

The answer is YES, though Android Volley we can perform a synchronous or blocking request. But first lets define a layout where all this action would happen.

To make an actual Android Volley blocking request lets define the activity:

In the above class I have started a standard java thread, on click of a button. This is the entry point of making a blocking request through volley. Since Android has a guideline that you can’t make network calls on main thread, it throws a network on main thread exception. I had to spawn a new thread, to make an environment where this synchronous type of request would actually come in use.

Please Note : In normal situations you should not be making this type of request.

After spawning a new thread called ThreadA, I started an AsyncTask (ThreadB) so that this can be put into waiting state, by calling get(long, TimeUnit) method of RequestFuture class. A volley class used for blocking requests. To access the get() method and make an Android Volley synchronous request, you need to use the RequestFuture class instead of standard StringRequest or JsonObjectRequest class. This RequestFuture class also implements both Response.Listener and Response.ErrorListener interfaces. Therefore it can be used as a listener as-well while making a request. The RequestFuture.get(long, TimeUnit) method returns the result immediately, as internally it calls the doGet(long) method, in a synchronized way. Hence instead of receiving the result back in onResponse(T response) method, we are actually waiting till the response is captured.

Android Volley Synchronous Request Diagram

Now since our AsyncTask (ThreadB) was also started in a blocking way by calling AsyncTask.get (long timeout, TimeUnit unit) method from ThreadA. Apparently ThreadA is also blocked at the moment. This is the key reason this cant be done on the main thread of your app. I tried doing it in the main thread and was not able to do it, as it used to throw a TimeoutException.

After completing the code, the screen should look something like the screenshot below:

Android Volley Synchronous Request

To understand the example better, please have a look at the full source code below:

Full Source Code

With this I would like to conclude my tutorial on how to make a blocking request with Android volley. If this tutorial helped you share it with your friends, also please like our Facebook, Google+ page.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

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

2 thoughts on “Android Volley | Making A Synchronous Request

  • Mark

    There is a problem with the RequestFuture get() method. If a response is delivered, in the onResponse method the notifyAll() method is called. This will cause an InterruptedException in the doGet() method. So the response is delivered but it causes an InterruptedException by request.get(). The only way to solve it is to call request.get() again when an InterruptedException happens.