当重试策略设置为 0 时,Android Volley 向服务器发出 2 个请求 [英] Android Volley makes 2 requests to the server when retry policy is set to 0

查看:27
本文介绍了当重试策略设置为 0 时,Android Volley 向服务器发出 2 个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 Volley 进行异步请求和图像缓存的 Android 项目.即使我将重试策略设置为 0,请求也会以某种方式命中服务器两次.我尝试覆盖 DefaultRetryPolicy 对象中的值但没有成功.下面是一些示例代码:

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:

请求:

@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;
                }


            }));
        }
    });
}

重试策略:

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

    return request;
}

基本上,我想将超时设置为 30 秒,重试 0 次.

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

如果我增加重试次数,它会按预期工作,但如果我将其设置为 0,则会发出 2 个请求.

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

这里需要一些帮助.

编辑

我设法通过在 android 中将 keep-alive 属性设置为 false 来解决我的问题.例如:

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

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

我在导入 requestqueue 并发出请求的类中添加了这行代码.

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

另外,请检查您的服务器是否具有 keep-alive 标头.

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

这篇帖子帮助找到了解决方案.

This post helped get to the solution.

推荐答案

DefaultRetryPolicy.class 的 hasAttemptRemaining() 类如下所示:

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

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

据我所知,如果还没有重试,将 maxNumRetries 设置为 0 仍然会使返回 true.

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

我用

request.setRetryPolicy(new DefaultRetryPolicy(0, -1, 0);

这篇关于当重试策略设置为 0 时,Android Volley 向服务器发出 2 个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆