WHAT IS VOLLEY ANDROID
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster.
ADVANTAGES OF USING VOLLEY
Automatic scheduling of network requests.
Multiple concurrent network connections.
Transparent disk and memory response caching with standard HTTP cache coherence.
Support for request prioritization.
Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
Debugging and tracing tools.
HOW TO USE VOLLEY LIBRARY IN ANDROID
STEP – 1
Create a new android project, go to your app-level build.gradle file and add the following dependency for using the volley library. To use Volley, you must add the android.permission.INTERNET permission to your app’s manifest.
STEP – 2
We’ll create a common class which can be used for making requests from any class we want. This class can also be used for checking phone’s internet connection. So, go ahead and create a class and name it as “VolleyClass”.
Basically, we need to create a RequestQueue and pass the request objects. The RequestQueue manages worker threads for running the network operations, reading from and writing to the cache, and parsing responses.
In our class, we’ll be creating a new request queue and pass the request objects. The Request object will be in the form of JSONObject Request.
Copy and paste the following code inside your VolleyClass. Please read the comments inside the code for a clear understanding of how the code works.
In the above code, VolleyResponseListener is an interface which contains the below code. Create a new interface “VolleyResponseListener” and put the following code inside it.
POST Method is used in the above class. You can always change it according to your need by calling Request.Method.GET or any other method in the constructor of JsonObjectRequest while creating request.
STEP – 3
Now, let’s see how do we use this VolleyClass to make a webserice request from the MainActivity. Go to onCreate() method of MainActivity and write the following code.
That’s it. Make sure to replace your webservice url and test.
Comment below for any issues. Thank you 🙂