Networking in Android with Volley library.

Volley was a networking library used by the Play store App, which was later open sourced in the Google I/O ’13 by Ficus Kirkpatrick.

Why should you use Volley?

  • It can do anything in android that has to do with networking.
  • Volley queues all the network requests automatically.
  • It provides transparent disk and memory caching.
  • It provides more customisation.
  • It has inbuilt debugging and tracing tools.
  • It has cancellation request api for cancelling single or multiple chunks of requests at a time.

Step 1:

You have to get the source of volley from

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

After downloading the source, import this project into your workspace and mark it as a library project.  Then add this library as a dependency of your app.

Step 2: Volley  Intro

There are two important class in volley that you have to note:

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

A Request object comes in three major types:

Step 3: Constructing a request queue

Every request in volley is first placed in a queue before they are processed. This is how you place your request in queue:

public MainActivity extends Activity {
	private RequestQueue mRequestQueue;

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

Step 4: Accessing a JSON Data

We can create a  Request  object which represents a desired request to be executed. Then we should add this request to the queue.

private void fetchJsonResponse() {                                                                   
		// Pass second argument as "null" for GET requests
JsonObjectRequest req = 
   new JsonObjectRequest("http://ip.jsontest.com/", null,
		        new Response.Listener<JSONObject>() {
		        @Override
		        public void onResponse(JSONObject response) {
		         try {
				           String result = "Your IP                       Address is " + response.getString("ip");
				Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
			           } catch (JSONException e) {
			              e.printStackTrace();
			           }
	           }
		    }, 
 new Response.ErrorListener() {
	@Override
	public void onErrorResponse(VolleyError error) {
				VolleyLog.e("Error:", error.getMessage());
			}
		});

		/* Add your Requests to the RequestQueue to execute */
		mRequestQueue.add(req);
	}

This will execute the request to the server  and respond back with the result as specified in the Response.Listener

To dive deep into the functionalities of volley follow these links:

The following two tabs change content below.

Amal Chandran

Latest posts by Amal Chandran (see all)

1 Comment

  1. Is it possible to store images captured from camera in server using volley library? How can I do it? HELP ME PLEASE….

Submit a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current month ye@r day *