Android Volley Example - Truiton
Skip to content

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:

compile ''

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:
    git clone

    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:

package com.truiton.volleyexample;

import android.content.Context;


 * Custom implementation of Volley Request Queue
public class CustomVolleyRequestQueue {

    private static CustomVolleyRequestQueue mInstance;
    private static Context mCtx;
    private RequestQueue mRequestQueue;

    private CustomVolleyRequestQueue(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

    public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new CustomVolleyRequestQueue(context);
        return mInstance;

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
            Network network = new BasicNetwork(new HurlStack());
            mRequestQueue = new RequestQueue(cache, network);
            // Don't forget to start the volley request queue
        return mRequestQueue;

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:

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

Next we would be making a custom JSON object request:

package com.truiton.volleyexample;


import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class CustomJSONObjectRequest extends JsonObjectRequest {

    public CustomJSONObjectRequest(int method, String url, JSONObject jsonRequest,
                                   Response.Listener<JSONObject> listener,
                                   Response.ErrorListener errorListener) {
        super(method, url, jsonRequest, listener, errorListener);

    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;

    public RetryPolicy getRetryPolicy() {
        // here you can write a custom retry policy
        return super.getRetryPolicy();

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.

package com.truiton.volleyexample;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import org.json.JSONException;
import org.json.JSONObject;

public class MainVolleyActivity extends AppCompatActivity implements Response.Listener,
        Response.ErrorListener {
    public static final String REQUEST_TAG = "MainVolleyActivity";
    private TextView mTextView;
    private Button mButton;
    private RequestQueue mQueue;

    protected void onCreate(Bundle savedInstanceState) {

        mTextView = (TextView) findViewById(;
        mButton = (Button) findViewById(;

    protected void onStart() {
        // Instantiate the RequestQueue.
        mQueue = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
        String url = ",uk";
        final CustomJSONObjectRequest jsonRequest = new CustomJSONObjectRequest(Request.Method
                .GET, url,
                new JSONObject(), this, this);

        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

    protected void onStop() {
        if (mQueue != null) {

    public void onErrorResponse(VolleyError error) {

    public void onResponse(Object response) {
        mTextView.setText("Response is: " + response);
        try {
            mTextView.setText(mTextView.getText() + "\n\n" + ((JSONObject) response).getString
        } catch (JSONException e) {

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:

<RelativeLayout xmlns:android=""

        android:layout_centerHorizontal="true" />

        android:text="Send Request"
        android:layout_alignEnd="@+id/imageView" />

        android:layout_alignParentEnd="true" />


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.

12 thoughts on “Android Volley Example”

  1. Android Studio: Adding a library to your project

    You may like to place the second step as File-> New-> Import Module -> Specify the directory

  2. Thank You Good explanation..
    Can you please add how to do two or more network requests..??
    As to how to handle responses from two or more network requests..

    Thanks in Advance

  3. hi Bro,
    if we add multiple request (Different response/Api) in mQueue, then how we will identify the response, is belong to which API/Response

Leave a Reply

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