Sunday, 4 December 2016

Volley library with all the methods in android

Volley offers the following benefits:

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


Update build.gradle file 

dependencies {
    ...
    compile 'com.mcxiaoke.volley:library:1.0.19'
}

Initialize Request Queue 

//Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = "Url goes here";

Making DELETE Requests

StringRequest deleteRequest = new StringRequest(Request.Method.DELETE, 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);
       }
    }
);
queue.add(deleteRequest);
 
 

Making PUT Requests 

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("parameter1", "value1");  
            params.put("parameter2", "value2");
            
            return params;  
    }
};
queue.add(putRequest);
 
 

Making POST Requests

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()
    {         // Adding parameters
            Map<String, String>  params = new HashMap<String, String>();  
            params.put("parameter1", "value1");  
            params.put("parameter2", "value2");
            
            return params;  
    }
};
queue.add(postRequest);
 
 

Making GET Requests

// 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) {
              //error
             Log.d("Error.Response", response);
       }
    }
);
// add it to the RequestQueue  
queue.add(getRequest);
 

 

 

 

No comments:

Post a Comment