Volley Library to make network connection from Android App

V

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 comes with lot of features. Some of them are

1. Effective request cache and memory management
2. Cancelling the requests

To use volley library just make below changes in your build.gradle file:

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

Now add below code to make network calls

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
               //get response 
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) { 
            }
        }) {
    @Override
    protected Map<String, String> getParams() {
           // send parameters for post
        return map;
    }

};

To set connection timeout add below code:
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
        10000,
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);





About the author

Ankita Deshmukh
By Ankita Deshmukh

Category