Skip to content

Networking with the Volley Library

Nathan Esquenazi edited this page Nov 7, 2013 · 38 revisions

Overview

Volley is a library that makes networking for Android apps easier and most importantly, faster. Volley Library was announced by Ficus Kirkpatrick at Google I/O '13! It was first used by the Play Store team in Play Store Application and then they released it as an Open Source Library.

Why Volley?

  • Volley can pretty much do everything with that has to do with Networking in Android. It has some built-in Requests, such as JsonObjectRequest, JsonArrayRequest, StringRequest etc, based on the generic type of the Request class inside the Library.
  • 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

Getting started

We need to install volley as a library project. First, download the volley source code:

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

and then we need to import the source code into our workspace and then mark it as a library. Now we can add this library as a dependency of any of our apps.

How to use Volley?

Volley has two classes that you will have to deal with:

  1. RequestQueue - Requests are queued up here to be executed
  2. Request (and any extension of it) - Constructing an network request

A Request object comes in three major types:

  • JsonObjectRequest — To send and receive JSON Object from the server
  • JsonArrayRequest — To receive JSON Array from the server
  • ImageRequest - To receive an image from the server
  • StringRequest — To retrieve response body as String (ideally if you intend to parse the response by yourself)

Constructing a request usually starts with one of these base types being created.

Constructing a RequestQueue

All requests in Volley are placed in a queue first and then processed, here is how you will be creating a request queue:

public MainActivity extends Activty{
	private RequestQueue mRequestQueue;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_screen_layout);
		// ...
		mRequestQueue = Volley.newRequestQueue(this);
		
	}
}

Accessing JSON Data

After this step you are ready to create your Request objects which represent a desired request.

public YourActivity extends Activity {

}

And that's it you are done :) !

Of course you can replace StringRequest with every other type of Request you want or just create your own!

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally