机器人排球使得2对服务器的请求时,重试策略被设置为0 [英] Android Volley makes 2 requests to the server when retry policy is set to 0

查看:219
本文介绍了机器人排球使得2对服务器的请求时,重试策略被设置为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的凌空异步请求和imagecaching的Andr​​oid项目。不知怎的,一个请求被击中两次服务器,即使我有重试策略设置为0,我试图从没有成功的DefaultRetryPolicy对象覆盖值。下面是一些示例code:

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

基本上,我想将暂停时以0重试次数设置为30秒。

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.

需要一些帮助在这里。

修改

我设法保持活动属性设置为false内部的Andr​​oid解决我的问题。例如:

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

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

我加入这行code的,我请求队列中导入,使请求的类中。

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.

这<一个href=\"http://stackoverflow.com/questions/3352424/httpurlconnection-openconnection-fails-second-time\">post帮到该溶液中。

推荐答案

我用这个设置不重复的政策:

I use this to set the no-repeat policy:

    myRequest.setRetryPolicy(new DefaultRetryPolicy(0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

这篇关于机器人排球使得2对服务器的请求时,重试策略被设置为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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