At Google I/O last year, the search engine giant showcased a faster way to make network requests within an Android application by introducing the Volley networking library. Volley includes a lot of features useful for managing network requests and their response caches in a fast and easy way.
One can use Volley to run simultaneous requests on a pool of threads that can be given priority over another. In addition, it can be used to handle JSON requests, image request or files of that size and nature.
In this tutorial, we will learn how to setup Volley for Android and write a sample code to make a JSON request.
One can use Volley to run simultaneous requests on a pool of threads that can be given priority over another. In addition, it can be used to handle JSON requests, image request or files of that size and nature.
In this tutorial, we will learn how to setup Volley for Android and write a sample code to make a JSON request.
Advantages of Volley library
a) Automatically schedules the network requests
b) Supports request prioritization. This means that you can load content depending of priorities, for example the main content could have a high priority, but the images a low priority.
c) Provides transparent disk and memory cache that allows for quick reloading of data. Transparent cache means that the caller doesn’t have to know about the existence of the cache. That is, the cache is implemented automatically. You do, however, have the possibility to disable the caching.
d) Provides a good API for canceling requests. You can cancel a single request, or cancel requests depending on some filters.
Besides the great features that Volley comes with, you don’t have to use it for everything. Volley is great for RPC-style network operations that populate UI, a typical example would be loading thumbnail images into a ListView, but not very good for streaming operations like downloading a video or music file
The following are the Important classes of Volley:
Topics covered in this Tutorial
-Making the GET Request
-Making POST Requests
-Making PuT Requests
-Making DELETE Requests
a) Automatically schedules the network requests
b) Supports request prioritization. This means that you can load content depending of priorities, for example the main content could have a high priority, but the images a low priority.
c) Provides transparent disk and memory cache that allows for quick reloading of data. Transparent cache means that the caller doesn’t have to know about the existence of the cache. That is, the cache is implemented automatically. You do, however, have the possibility to disable the caching.
d) Provides a good API for canceling requests. You can cancel a single request, or cancel requests depending on some filters.
Besides the great features that Volley comes with, you don’t have to use it for everything. Volley is great for RPC-style network operations that populate UI, a typical example would be loading thumbnail images into a ListView, but not very good for streaming operations like downloading a video or music file
The following are the Important 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.
- - JsonObjectRequest: HTTP Request where the response is JSONObject.
Topics covered in this Tutorial
-Making the GET Request
-Making POST Requests
-Making PuT Requests
-Making DELETE Requests
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).
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).
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.
For a POST request, to add form parameters/values, the getParams() method needs to be overridden and a Map needs to be returned.
Making PUT Requests
Creating PUT Request is same as POST basically.
Creating PUT Request is same as POST basically.
Making DELETE Requests
Creating the delete request is as follows
Creating the delete request is as follows
Sample source code for Volley lib is available at gitHub following link: https://github.com/shir60bhushan/AndroidVolleyLibraryTutorial