Android Volley makes 2 requests to the server when retry policy is set to 0
Asked Answered
D

3

17

I'm working on an Android project that uses Volley for async requests and imagecaching. Somehow a request is hitting the server twice even when I have the retry policy set to 0. I tried overriding the values from the DefaultRetryPolicy object with no success. Here's some sample code:

The request:

@Override
public void gravarVendaMobile(final Usuario usuarioAutenticado, final AsyncCallback<String> callback) {
    obterParametrosDeInicializacao().done(new DoneCallback<ParametrosDeInicializacao>() {
        @Override
        public void onDone(final ParametrosDeInicializacao param) {
            requestQueue.add(setDefaultRetryPolicy(new StringRequest(
                    Method.POST,
                    urlPara(GRAVAR_VENDA_MOBILE, usuarioAutenticado.getFilial(), usuarioAutenticado.getCodigo()),
                    listener(callback),
                    //errorListener(R.string.could_not_load_produtos, callback)
                    new ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            callback.onError(new MessageCodeException(error.networkResponse.statusCode, error));
                        }
                    }
            ) {

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<String, String>();
                    headers.put("Encoding", "UTF-8");
                    headers.put("Accept", "application/json");
                    headers.put("Content-type", "application/json; charset=UTF-8");
                    return headers;
                }


            }));
        }
    });
}

Retry Policy:

private Request<?> setDefaultRetryPolicy(Request<?> request) {
    request.setRetryPolicy(new DefaultRetryPolicy(30000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    return request;
}

Basically, I want to set the timeout to 30 secs with 0 retries.

If I increase the number of retries it works as expected, but if I set it to 0 it makes 2 requests.

Need some help here.

Edit

I managed to solve my issue by setting the keep-alive property to false inside android. eg:

System.setProperty("http.keepAlive", "false");

I added this line of code inside the class where I import requestqueue and make the requests.

Also, check if you server has the keep-alive header.

This post helped get to the solution.

Dressel answered 8/10, 2014 at 19:32 Comment(3)
You should post your edit as a separate answer, since it is the solution to this question you were looking for (self-answers are allowed).Flicker
I am experiencing the same issue. I increased timeout to 60 secs, because retries keep going twice even though I have DefaultRetryPolicy(60000, 0, 0). Why?Tabaret
this is for sure not an answer to your issue, but a side effect that causes the issue not to happen. there are 2 things not clear: 1) setDefaultRetryPolicy you pasted is called where and when as Volley's ImageRequest sets its own RetryPolicy 2) which is the http stack you use because HURL is using okhttp for some time and it has silent retry on requests Answering those could lead to really find why this is happeningCoequal
N
2

The DefaultRetryPolicy.class's hasAttemptRemaining() class looks like this:

protected boolean hasAttemptRemaining() {
    return this.mCurrentRetryCount <= this.mMaxNumRetries;
}

From what I can see, setting the maxNumRetries to 0 will still make that return true if it hasn't done a retry yet.

I fixed it with a

request.setRetryPolicy(new DefaultRetryPolicy(0, -1, 0);
Nectarous answered 16/12, 2016 at 20:51 Comment(1)
Except that the mCurrentRetryCount is incremented in retry before calling hasAttemptRemaining(), so setting the retry count to 0 works properly here.Uprear
P
1

I use this to set the no-repeat policy:

    myRequest.setRetryPolicy(new DefaultRetryPolicy(0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Padrone answered 14/7, 2016 at 15:41 Comment(0)
G
0

Just set timeout to 20second or more as you want,and retry to 0

req.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0,
  DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Gros answered 21/7, 2016 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.