About Paresh Mayani

Paresh Mayani is a Mobile application developer from India, having been involved in Android app development since around 3 years. He writes technical articles at TechnoTalkative. Apart from his job, he manages Google Developer Group (GDG) - Ahmedabad and has been speaker in various events. He is very much active in supporting the Android developer community, from answering questions on StackOverflow to publishing articles with possible sample code. Currently he is holder of around 25000 reputation.

Android – Volley library example

I am not sure whether you have heard “Volley” word yet but it’s the library on which one expert talk was delivered during Google I/O 2013 by Ficus Kirkpatrick.

What is Volley library exactly for?

Volley is a library that makes networking for Android apps easier and most importantly, faster. It manages the processing and caching of network requests and it saves developers valuable time from writing the same network call/cache code again and again. And one more benefit of having less code is less number of bugs and that’s all developers want and aim for.

My mean of writing the same network call code is AsyncTask and the logic/code you write for fetching response from Web API and displaying it in particular View. We have to take care of displaying ProgressBar/ProgressDialog inside onPreExecute() and onPostExecute(). I know this is not a hard task but still boring, sometimes I also feel get bored even though I have defined BaseTask class for managing display/dismiss operation of ProgressBar/ProgressDialog and many more thing. So now we can say Volley can be a powerful alternative of AsyncTask.

Advantages of using Volley:

Volley automatically schedule all network requests. It means that Volley will be taking care of all the network requests your app executes for fetching response or image from web.

Volley provides transparent disk and memory caching.

Volley provides powerful cancellation request API. It means that you can cancel a single request or you can set blocks or scopes of requests to cancel.

Volley provides powerful customization abilities.

Volley provides Debugging and tracing tools

How to get started?

Clone the Volley project

Import the code into your project

Clone the Volley project:

git clone https://android.googlesource.com/platform/frameworks/volley

It has created “Volley” folder. Now we have to import this in eclipse or Android studio. FYI, I have been using Git GUI client on my windows machine.

Now suppose If your machine is not having Git client installed and still want to clone repository then Eclipse and ADT Bundle is having option for you to clone repository and import project directly. Refer my previous article for the same: Android – Import projects from Git

2 Main classes of Volley:

There are 2 main classes:

Request queue

Request

Request queue: It is the interest you use for dispatching requests to the network, you can make a request queue on demand if you want, but typically, you’ll instead create it early on, at startup time, and keep it around and use it as a Singleton.

Request: It contains all the necessary details for making web API call. For example: which method to Use (GET or POST), request data to pass, response listener, error listener.

Take a look at JSONObjectRequest request method:

Basic example using Volley:

I assume you have already cloned/downloaded Volley library from git repo. Now, follow the steps to create a simple example of fetching tweets and display it into ListView.

Step 1: Make sure you have imported Volley projects into Eclipse, if you haven’t then import it. Now after importing, we need to make it a Library project by Right click => Properties => Android (left panel).

Step 2: Now, Create a new project with the name VolleyExample.Step 3: Right click on VolleyExample and Include Volley Library in our project.

and many more ....

2 comments

Hi Paresh, First of all, Thank you for this very simple tutorial for Volley. I just want to know, how can I make a Post request ? Yes ,there I can change Request.Method to POST but what about entities need to be added while making request of, like A Login Web Service need login id and password. Please guide me for the same.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.