如何防止Volley请求重试? [英] How to prevent Volley request from retrying?

查看:92
本文介绍了如何防止Volley请求重试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将JsonRequest发布到服务器.服务器速度很慢,Volley倾向于多次调用速度较慢的服务器,因为它没有收到第一个请求的响应(因为我的服务器速度很慢).有没有一种方法可以防止Volley重试请求,使其能够收到第一个响应?

I post a JsonRequest to a server. The server is slow and Volley tends to make multiple calls to the slow server because it didn't get a response from the first request (since my server is slow). Is there a way to prevent Volley from retrying a request so that it can receive the first response?

我尝试过:

myRequest.setRetryPolicy(new DefaultRetryPolicy(
                TIMEOUT_MS, 
                RETRIES, 
                BACKOFF_MULT)); 

我已将TIMEOUT_MS替换为0,将RETRIES替换为0,并将BACKOFF_MULT替换为0,但是没有用.

I have replaced TIMEOUT_MS with 0, RETRIES with 0, and also BACKOFF_MULT with 0, but it didn't work.

有什么建议吗?

推荐答案

Volley默认重试策略为:

The Volley Default Retry Policy is:

/** The default socket timeout in milliseconds */
public static final int DEFAULT_TIMEOUT_MS = 2500;

/** The default number of retries */
public static final int DEFAULT_MAX_RETRIES = 1;

/** The default backoff multiplier */
public static final float DEFAULT_BACKOFF_MULT = 1f;

您可以在DefaultRetryPolicy.java中找到它,

You can find it in DefaultRetryPolicy.java,

因此您可以看到默认情况下齐射发出1个重试请求.

so you can see that volley makes 1 retry request by default.

尝试使用较小的TIMEOUT(如果您不想等待2500ms),或者使用大于2500ms的时间来获取答案),但请保留其他值,例如:

Try to use smaller TIMEOUT (if you don't want to wait the 2500ms), or bigger than 2500ms to get the answer), but keep the other values, for example:

// Wait 20 seconds and don't retry more than once
myRequest.setRetryPolicy(new DefaultRetryPolicy(
       (int) TimeUnit.SECONDS.toMillis(20),
       DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
       DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

因此,要禁止Volley重试,您需要执行以下操作:

Hence, to disable Volley from retrying you will need to do:

myRequest.setRetryPolicy(new DefaultRetryPolicy(
       (int) TimeUnit.SECONDS.toMillis(20), //After the set time elapses the request will timeout
       0,
       DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

这篇关于如何防止Volley请求重试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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