Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retry request onErrorResponse Android volley

When i receive an error in onerrorrepsonse of android volley request i want to retry the request. How can i achieve this?

like image 538
user3009752 Avatar asked Jul 10 '14 12:07

user3009752


1 Answers

well, you can create the RetryPolicy to change default retry behavior, only specify timeout milliseconds, retry count arguments :

public class YourRequest extends StringRequest {
    public YourRequest(String url, Response.Listener<String> listener,
                       Response.ErrorListener errorListener) {
        super(url, listener, errorListener);
        setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    }
}

the another way is estimate the VolleyError, re-execute the same request again when if was TimeoutError instance :

public static void executeRequest() {
    RequestQueue.add(new YourRequest("http://your.url.com/", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (error instanceof TimeoutError) {
                // note : may cause recursive invoke if always timeout.
                executeRequest();
            }
        }
    }));
}

you may have a question at this very moment : "have Volley offer some retry callback methods ?", the answer is "none". but there have a project calls Netroid which based Volley and satisfy preceded question, with it, you can take a retry callback if you care about that, you can calculate how much time used when retry coming and how long this request execute, the code style like this :

final String REQUESTS_TAG = "Request-Demo";
String url = "http://facebook.com/";
JsonObjectRequest request = new JsonObjectRequest(url, null, new Listener<JSONObject>() {
    long startTimeMs;
    int retryCount;

    @Override
    public void onPreExecute() {
        startTimeMs = SystemClock.elapsedRealtime();
    }

    @Override
    public void onFinish() {
        RequestQueue.add(request);
        NetroidLog.e(REQUESTS_TAG);
    }

    @Override
    public void onRetry() {
        long executedTime = SystemClock.elapsedRealtime() - startTimeMs;
        if (++retryCount > 5 || executedTime > 30000) {
            NetroidLog.e("retryCount : " + retryCount + " executedTime : " + executedTime);
            mQueue.cancelAll(REQUESTS_TAG);
        } else {
            NetroidLog.e(REQUESTS_TAG);
        }
    }
});

request.setRetryPolicy(new DefaultRetryPolicy(5000, 20, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
request.setTag(REQUESTS_TAG);
RequestQueue.add(request);

Netroid also have many other handy and powerful features, hope that will help you enough :).

like image 52
VinceStyling Avatar answered Nov 14 '22 18:11

VinceStyling