Skip to content Skip to sidebar Skip to footer

Where Should I Write Setretrypolicy() Method Call When Using Volley For Android Development

It might be a simple question but I tested it in actual code and unable to judge the correct behaviour of setRetryPolicy() function of Volley. Anyone please tell me the correct pos

Solution 1:

Add the retry policy once you have declared and initialized the Request object. It's okay to add the policy anywhere before adding your request to the Volley queue.

ImageRequest  ir = newImageRequest(url, newResponse.Listener() {

        @OverridepublicvoidonResponse(Bitmap response) {
            iv.setImageBitmap(response);
        }
    }, 0, 0, null, newResponse.ErrorListener() {

        @OverridepublicvoidonErrorResponse(VolleyError error) {
            //Handle errors related to Volley such as networking issues, etc
        }
});

ir.setRetryPolicy(newDefaultRetryPolicy(20 * 1000, 2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
mRequestQueue.add(ir);

Another note: The onErrorResponse() callback function is used to handle errors generated from Volley. At this point, your request is already dispatched and got some networking error. Otherwise, your code would not reach this callback function. So, it's pointless to add the retry policy inside this function.

Post a Comment for "Where Should I Write Setretrypolicy() Method Call When Using Volley For Android Development"