Fast way to do network Operation using Volley Library

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. 
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:


  • - 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).


 
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
    new Response.Listener() 
    {
        @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 PUT Requests
Creating PUT Request is same as POST basically.
 
StringRequest putRequest = new StringRequest(Request.Method.PUT, url, 
    new Response.Listener() 
    {
        @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 getParams() 
    {  
            Map  params = new HashMap ();  
            params.put("name", "Alif");  
            params.put("domain", "http://itsalif.info");
             
            return params;  
    }
 
};
queue.add(putRequest);

Making DELETE Requests
Creating the delete request is as follows.
 

StringRequest dr = new StringRequest(Request.Method.DELETE, url, 
    new Response.Listener() 
    {
        @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);
 

source code  link: https://github.com/shir60bhushan/AndroidVolleyLibraryTutorial
Post a Comment