Android Volley Example


Photo Credits: Simone : Android Volley Example - Featured

Most of the android apps are a subset of their web apps. Other type of android applications are those which use network to store some data online like most of the games. Therefore majority of the android applications perform network interactions. Like consuming web services, uploading data etc. Being an android developer you must know that you cant perform network interactions on main thread. As it throws an android.os.NetworkOnMainThreadException. To ease the development, a new framework was introduced recently, called Android Volley Framework. This framework is available through the open AOSP repository. In this tutorial we will make a working Android Volley Example.

This Framework was designed keeping in mind that in Android network requests are not performed on main thread. Hence Android Volley performs all the network operations in worker threads, still it makes the process of making an API call to the network very easy. In this tutorial I would show a simple Android Volley Example. Until now no such framework was available in Android through which one can perform network requests with such minimal coding.

Android Studio: Adding a library to your project

Android Volley framework is distributed in form of a library project which is available at the open AOSP repository. As of now there are two ways to include and use Volley in your project. You can use either one of them. But as expected, the Library project would always have the latest code, and on the contrary side gradle dependency way would by very easy to use:

Gradle Dependency

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

 

Include project like a library

Steps for downloading it and adding it to your project in Android Studio 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 go to File-> New-> 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.

Making an Android Volley Example

Earlier in Android to make a webservice or an API call we were supposed to use DefaultHttpClient class. Through which an HTTP request was executed. But now through this Android Volley example we would learn a way through which asynchronous HTTP REST API calls can be made. Volley library makes it very easy for developers to customize and prioritize requests. It also provides a method through which, one can cancel a single or multiple requests that are in request queue.

The interesting part of volley framework is that, it is pre configured to support multiple types of requests like:

  1. JSON Object Request – Through class JsonObjectRequest.
  2. JSON Array Request – Through class JsonArrayRequest.
  3. String Request – Through class StringRequest.
  4. Image Transfer Request – Through class ImageRequest.

Most commonly used data type for an API call nowadays is a JSON object. Therefore in this Android Volley Example I would show how to send a JSON object request. To do so we will create a custom volley request queue in a singleton pattern.

Android Volley Request Queue

In volley framework one of the most important classes is the RequestQueue class. This class works in a FIFO (First In First Out) manner. That is when a request in added in the queue, it is inserted at the tail, and at time of execution, request is pulled out from the front.

Android Volley Multiple Requests : How Many Requests?

A question that would come in every developers mind is that, at a time how many requests can android volley make? The ans is, at a time volley can execute 4 requests simultaneously. In default implementation of volley network thread pool size is 4:

Android Volley Caching of Requests

Another interesting feature of volley is that when a request queue is created, and a request is added in it. It is not like that this request would be sent to a worker thread immediately for execution. First it is checked in the Android Volley cache of requests. Is this request is in the waiting request queue at the moment or not? This check is made through the URL of this request. If they match this new request is not executed and old request is replaced with new request object.

Further lets have a look at our custom volley request queue class:

Here in the class above we have used a singleton pattern for RequestQueue implementation. So that at a time only one request queue can exist in the application. This class is should be initialized with the application context, by doing this we would make this queue last till the app lasts. An important thing to note here is that we are making a custom cache in this Android Volley Example of 10 MB. The default implementation only allows for 5 MB of cache.

Before making a request queue we may need to do some basic things to make this Android Volley Example to work.

Add this in manifest:

Next we would be making a custom JSON object request:

The above class is a custom JsonObjectRequest for volley example. Mostly when working for enterprise applications we need to specify some default parameters, like Accept, and various other authentication headers. Therefore instead of adding headers in every request one can use the getHeaders() method to specify the default headers which are required to be sent in every request. In this class you can also write a custom retry policy for your request by using the method getRetryPolicy(). Also this will be applicable on all the requests.

Next lets define the main activity class where all the above classes would come into action.

In the above activity class we are simply creating a custom volley RequestQueue and a custom JsonObjectRequest, with a tag. After the request is created, on click of a button it is added into the custom RequestQueue through the method .add(). Once the add method is called the request is queued for execution.

Another feature of volley is that, a request can be cancelled at anytime by calling a method cancel() on the CustomJSONObjectRequest. Or if you have specified a tag for a request, like in the class above, you can cancel all the requests for that tag with a single method call cancelAll(). This method can be called from the RequestQueue.

We have a simple layout for the above activity:

This would look something like this :

Android Volley Example

To have a better understanding, please have a look at the full source code for this example:

Full Source Code

With this we would like to conclude this Android Volley Example. If it helped you please share it with your friends and 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 *

12 thoughts on “Android Volley Example