请求缓慢时,Android Volley 双发布 [英] Android Volley double post when have slow request

查看:29
本文介绍了请求缓慢时,Android Volley 双发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在慢速网络上遇到 Volley POST 请求的问题.每次我在 LogCat 中看到 BasicNetwork.logSlowRequests 时,我的 POST 请求都会执行两次或更多次,从而导致 1 个请求的多个(2 个或更多)张贴.我已经将重试策略设置为 0,但它没有帮助.

I have a problem with Volley POST request on slow network. Everytime I see BasicNetwork.logSlowRequests in my LogCat, my POST request is executed twice or more resulting multiple (2 or more) postings for 1 request. I already set the retry policy to 0, but It doesn't help.

这是我的 LogCat

This is my LogCat

03-16 01:31:35.674: D/Volley(5984): [19807]BasicNetwork.logSlowRequests:请求的 HTTP 响应=<[]http://[myserver]/api/places 0xfa7d0c33 NORMAL 1> [lifetime=3824],[size=313], [rc=200], [retryCount=0] 03-16 01:31:35.704:D/Volley(5984): [1] Request.finish: 3853 ms: [ ]http://[myserver]/api/places 0xfa7d0c33 正常 1

03-16 01:31:35.674: D/Volley(5984): [19807] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://[myserver]/api/places 0xfa7d0c33 NORMAL 1> [lifetime=3824], [size=313], [rc=200], [retryCount=0] 03-16 01:31:35.704: D/Volley(5984): [1] Request.finish: 3853 ms: [ ] http://[myserver]/api/places 0xfa7d0c33 NORMAL 1

这是我的代码

JSONObject body = new JSONObject();
try {
    body.put(PROTO_BODY_AUTHORIZATION, Sessions.getActiveSession().getToken());
} catch (JSONException e) {
    e.printStackTrace();
}

JsonObjectRequest request = new JsonObjectRequest(
        Request.Method.POST,
        context.getResources().getString(R.string.server_address) + "/places",
        body,
        callback,
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
);

request.setRetryPolicy(
        new DefaultRetryPolicy(
                DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
                0,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    getRequestQueue().add(request);

请帮忙,我正在拼命寻找这个问题的解决方案.

Please help, I desperately finding solution for this problem.

推荐答案

将以下值添加到您的请求对象:

Add the following values to your Request object:

request.setRetryPolicy(new DefaultRetryPolicy(
    DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

这里:request 是您的 JsonObjectRequest 对象.根据 Volley 中 DefaultRetryPolicy 类中的 DEFAULT TIMEOUT VALUE 更改乘法器的值.

Here: request is your object of JsonObjectRequest. Change the value of multiplicator according to the DEFAULT TIMEOUT VALUE in DefaultRetryPolicy class in Volley.

您也可以将第一个参数设置为 0,如下所示:

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

这篇关于请求缓慢时,Android Volley 双发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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