Android Working with Volley Library – Http Requests
Volley Library is great when it comes to call a web api and control it’s response with a very easy way. We often need to call the web api to retrieve the data from network or a database. We have many choices to call the api but there is a difference in every method. I have researched and found volley library by google is more efficient and easy way to call web apis. It approximately takes 200-300 ms to call a web api.
Android volley is a networking library was introduced to make networking calls much easier, faster without writing tons of code. By default all the volley network calls works asynchronously, so we don’t have to worry about using asynctask anymore.
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.
Following are the important class 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.
To use Volley, you must add the android.permission.INTERNET
permission to your app’s manifest . Without this, your app won’t be able to connect to the network.
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 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);
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 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 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);
Enjoy Coding and Share Knowledge