Android Volley Tutorial – Making HTTP GET, POST, PUT

Google released Android Volley Library around May/June 2013, which has been internally used by Google for some time. It is supposed to provide Fast Networking Operations and also takes care of Threads nicely. If you are unfamiliar with Volley, please see Google I/O 2013 Video

Unfortunately, there is almost no documentation on Volley. So, I put together code snippets on how to make Volley HTTP Requests (GET, POST, PUT, DELETE).

Setting up Android Volley Library

Setting up is straight-forward. Clone the Volley project from here and then import the Volley into project. A comprehensive tutorial on setting up can be found here.

Key Classes of Android Volley

The following are the Key classes of Volley:

  • – RequestQueue: A Queue containing the Network/HTTP Requests that needs to be made.
  • – Request: A Base Class which contains Network related information like HTTP Methods.
  • – StringRequest: HTTP Request where the response is parsed a String. View Source
  • – JsonObjectRequest: HTTP Request where the response is JSONObject. View Source

Getting Started with Android Volley

At first make a RequestQueue, which holds the HTTP Requests. View Source. Ideally, the RequestQueue should be made once and then referred to it across the Application. The Application is an ideal place to make it.

RequestQueue queue = Volley.newRequestQueue(this);  // this = context

Making GET Requests

Making GET Requests is simple. The example below uses JsonObjectRequest. It prepares a JsonObjectRequest and
passes and then adds it to RequestQueue. The JsonObject accepts 4 parameters (Http method, Url, Json values, Response Listener – Invoked on success, Error Listener – Invoked on failure).


final String url = "http://httpbin.org/get?param1=hello";

// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
	new Response.Listener<JSONObject>() 
	{
		@Override
		public void onResponse(JSONObject response) {	
                        // display response		
			Log.d("Response", response.toString());
		}
	}, 
	new Response.ErrorListener() 
	{
		 @Override
		 public void onErrorResponse(VolleyError error) {			 
			Log.d("Error.Response", response);
	   }
	}
);

// add it to the RequestQueue	
queue.add(getRequest);

Making POST Requests

For a POST request, to add form parameters/values, the getParams() method needs to be overridden and a Map needs to be returned.

url = "http://httpbin.org/post";
StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
	new Response.Listener<String>() 
	{
		@Override
		public void onResponse(String response) {
			// response
			Log.d("Response", response);
		}
	}, 
	new Response.ErrorListener() 
	{
		 @Override
		 public void onErrorResponse(VolleyError error) {
			 // error
			 Log.d("Error.Response", response);
	   }
	}
) {		
	@Override
	protected Map<String, String> getParams() 
	{  
			Map<String, String>  params = new HashMap<String, String>();  
			params.put("name", "Alif");  
			params.put("domain", "http://itsalif.info");
			
			return params;  
	}
};
queue.add(postRequest);

Making PUT Requests

Creating PUT Request is same as POST basically.

url = "http://httpbin.org/put";
StringRequest putRequest = new StringRequest(Request.Method.PUT, url, 
	new Response.Listener<String>() 
	{
		@Override
		public void onResponse(String response) {
			// response
			Log.d("Response", response);
		}
	}, 
	new Response.ErrorListener() 
	{
		 @Override
		 public void onErrorResponse(VolleyError error) {
                         // error
			 Log.d("Error.Response", response);
	   }
	}
) {

	@Override
	protected Map<String, String> getParams() 
	{  
			Map<String, String>  params = new HashMap<String, String> ();  
			params.put("name", "Alif");  
			params.put("domain", "http://itsalif.info");
			
			return params;  
	}

};

queue.add(putRequest);

Making DELETE Requests

url = "http://httpbin.org/delete";
StringRequest dr = new StringRequest(Request.Method.DELETE, url, 
	new Response.Listener<String>() 
	{
		@Override
		public void onResponse(String response) {
			// response
			Toast.makeText($this, response, Toast.LENGTH_LONG).show();
		}
	}, 
	new Response.ErrorListener() 
	{
		 @Override
		 public void onErrorResponse(VolleyError error) {
			 // error.
			 
	   }
	}
);
queue.add(dr);

Hope it helps!

  • Daniyal Butt

    change address with localhost/ . done

    “http://httpbin.org/post” -> http://localhost/post

  • Nitin Jadhav

    nice very helpful

  • Rahul

    Very Helpful Sir. Appreciated

  • candy

    nice …but can you tell me how i can send a variable to the php url by volley when click on listview item each listview item show different data related to that variable send by volley and load that data to new activity?

    • arief zainuri

      you can use intent.putExtra(“key”, String dataYouWantToSend);
      and get the extra using getIntent.getStringExtra(“key”);

  • hcbsaab

    I need help for PATCH requests with volley, can anyone help?

  • https://plus.google.com/100566630691752307059/posts Scott Linford

    Just right. The brace matching style makes it clear. Makes the complex easier to read.

  • ibrahim nehme

    How can you pass “param1=hello” in your first example no part of the url?

  • pammy

    Thanks. It’s very clear and straight forward

  • faisal

    thank you, your tutorial works

  • http://cochinwebhost.in CochinWeb

    Hi, thanks for your tutorial. I have an issue. How can i update the data from server after an action on another activity?

  • Imam Aly

    how to GET request with id as parameter?

    • Renato Mello Konflanz

      Read again the tutorial, observe the function “getParams()”. To use the ID, just add the id in the params: “params.put(“id”, “1”);”

  • Moch. Faisal Rasid

    very nice, i already success use GET with this tutorial

  • Rodrigo Mayer

    Thanks for the tutorial man! It has helped a lot!

  • Jaydeep Bhayani

    how to do “Https” request using volley??

  • Muhammad Sikandar

    hi bro i want to knw that how to fetch a specific row from mysql database by using the primary key column using volly .

  • Moises Cardona Lopez

    Hi, thanks a lot for your help, This is a amazing Tutorial. Best Greetings

  • Andy Mubalama

    Thanks for the tutorial

  • Zubair Shahzad Arain

    nice

  • Mahyar Mohammadi

    Thanks. This tutorial is so helpful.

  • Prudvi Raju

    thanks for your tutorial…

  • Jenny Casarino

    onErrorRepose(VolleyError error)
    how is it logging Log.d(“Error.Response”, response);?

    where does the response variable declared?