Android Volley Library

Volley is an HTTP Networking Library for Android which makes Networking faster, easier & less complex. It was introduced in Google IO 2013 by Ficus Kirkpatrick. It is available on AOSP repository.

Before Android Volley :

Before Volley implementing networking was a headache for developers. Most of the developers used Java class java.net.HttpURLConnection and the Apache org.apache.http.client to make REST API, to perform network operation in separate thread they used Asynctask.

Problem with Asynctask , HttpURLConnection & HttpClient : 

  • HttpUrlConnection and HttpClient were not perfect with Android GingerBread and Lower
  • HttpClient is no longer maintained
  • Asynctask has a Memory Leak issues
  • Asynctask can not Prioritisation request order
  • Use of MultiThreading was tough for many developers
  • Caching needs to be handled separately

To overcome these overhead Android needed a good Networking Library which can be fast, reliable & time saving

Features of Volley :

  • Automatic Scheduling of Network Request
  • Setup a Request Queue
  • Multiple Concurrent Requests
  • Disk and Memory Response Cache
  • Request Prioritisation
  • Ongoing and Pending Request Cancellation
  • Support for HTTP Request Methods like GET, POST, PUT, DELETE 
  • Get Response in String, JSON & Image
  • Customizable as per your needs

 

How do I setup Android Volley Library?

Unlike something you can not just import the jar of volley and start developing with it. Setting up Volley is a bit tedious process

1> Import Volley Source as Android Module :

Download Volley Source code from git, here is the git,

Now, In Android Studio, with your project opened, select File > New Module, and choose Import Existing Project. Select the directory where you’ve just downloaded the source code and confirm. A folder named Volley will show up in your project structure. Android Studio automatically updates your settings.gradle file to include the Volley module so you just need to add to your dependencies compile project(‘:volley’) and you’re good to go.

2> Add Volley Dependency into gradle file:

Note: This is a copy of official Volley source and now it is no longer maintained.

 

Using Android Volley Library :

With Volley we can make,

  • StringRequest
  • JsonRequest
  • ImageRequest

Making a Simple String Request :

Making a Image Request:

Making a JSON Request:

Volley provides JsonArrayRequest and JsonObjectRequest classes to make & handle JSON Request.

Available HTTP Request Methods :

You can use following HTTP methods to make requests

Setting Request Priorities:

Suppose if you are developing a news application the article is more important compare to the Images, So you need to set priorities in order to load text data before the images & thumbnails.

You can set request priorities based on your requirements by overriding getPriority() method from Volley’s Request Class,

There are typically 4 priorities are available to choose from,

Sending Parameters with Request:

Sometime we need to pass parameters to with our request, parameters can be anything from username, password, product-id, limit value etc…
Volley provides <Key, Value> type Map to pass parameter with the request. We need to override getParams() method from Volley’s Request Class.

Cancelling Request From Queue:

Volley provides powerful cancellation API. You can cancel ongoing request as well as pending request from Volley. We will see how to cancel single request in our next tutorials but to cancel all request from request queue

 

To make this things more understandable I will not cover ImageCache and Volley Singleton pattern in this tutorial, we will implement that on next tutorials.

How to make XML Request with Volley?

Currently Volley does not provide XML support out of the box, but there is a way you can still make an XML request without writing too much code, I will explain you in future tutorials.

Make comment if you have any questions regarding this Tutorial. This is just an explanation about Volley, I will provide you working example in my next tutorial.

1 Comment

Leave a Reply

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