Android Volley is Asynchronous HTTP Requests library which helps to make network calls very efficiently and easily. Volley is faster and it takes less than a minute to setup volley into your project. You don’t need to write long code to make the call and write Async task to make network calls, Volley handle all this by its own and the best part it create other thread and you can add no of calls to the server by adding request into the queue.

 

Android Volley tutorial

Most useful features of Android Volley

  1. Automatic scheduling of network requests.
  2. Effective request cache and memory management
  3. Support for request prioritization.
  4. Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.

To read more feature of volley library you can read this official Google Volley post.

Installing and using Android Volley in project

Step 1. Create a project in your Android Studio and add Volley dependencies in your build.gradle

after adding dependence you can synchronize your project to install dependence. 

Step 2. Now we can create a new java class in our project which extends Application this is our main application in which we can initialize Volley and also create a new method in which we can add request queue and cancel request as show in below  code

Making asynchronous HTTP requests with Volley

Volley provides the following utility classes which you can use to make asynchronous HTTP requests:

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

Create your first JsonObjectRequest and set header

Before we start, please keep one thing in mind the request is based on the data which you get from web service if you get JsonObject in starting of service as shown below then you can use JsonObjectRequest 

But if your service contains JsonArray then you need to make JsonArrayRequest

If you’re not sure then you can simply use String Request.

Now edit your MainActivity.java and inside your onCreate we can make our first call

In above code first, we create JsonObjectRequest object and we pass some parameter in it as shown below

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request_METHOD, WEB_API_URL, CUSTOM_REQUEST_PARAMETER_ELSE_NULL, HANDEL_RESPONSE_ERROR){ SET_HEADER|SET_PARAMETER }

If you want to pass header with your request you can override getHeaders() method as shown in above code. We can call volley object from our main application and add or request in queue

Create JsonArrayRequest using Volley

JsonArrayRequest is same as JsonObjectRequest only you need to replace JsonObjectRequest With JsonArrayRequest but keep in mind you response data in JsonArray format.

Create String Request in Volley

String request is used when you want to load data as a string.

Post form data using Volley

If you want to post form data to the server you need to create custom request and pass it to request function as shown below.

Cancelling Volley Request

In Volley Android library there is two way to cancel request first we can cancel only selected request and other we can cancel all the requests. If you remember addToRequestQueue() which we create in our main application class have two parameters one for the request and other for TAG, with the help of this tag we can cancel selected request as shown below.

Cancel single request in Volley

Cancel all requests in Volley

To cancel of all request in your queue you just need to call cancelAll() it cancel all your request.

Demo: I use volley library in one of my e-commerce application Offloo

If this post helps you to understand working with Android Volley library, then please subscribe, our blog Also do Like our Facebook Page or Add us on Twitter.