backoffMultiplier在DefaultRetryPolicy中的含义是什么?

Ras*_*ain 12 android android-volley

我创建了新的Job重试策略: new DefaultRetryPolicy(5000, 2, 2);

那么,backoffMultiplier意味着什么DefaultRetryPolicy

Ras*_*ain 11

RetryPolicy处理这三个参数

超时 - 指定每次重试尝试的套接字超时(毫秒).

重试次数 - 尝试重试的次数.

BackOff Multiplier - 一个乘数,用于确定每次重试尝试设置为套接字的指数时间.

对于上面的例子

超时 - 5000秒,尝试次数 - 2,后退乘数 - 2

尝试1:

time = time +(time*Back Off Multiplier);

时间= 5000 + 10000 = 15000

套接字超时=时间;

使用15秒的套接字超时调度请求

尝试2:

time = time +(time*Back Off Multiplier);

时间= 15000 + 30000 = 45000

套接字超时=时间;

使用45秒的套接字超时调度请求

因此,在尝试2结束时,如果仍然发生Socket超时,Volley将在您的UI错误响应处理程序中抛出TimeoutError.

  • 答案是从这里复制/粘贴 http://beginners-java.blogspot.fi/2015/03/customize-volley-default-retry-policy.html (2认同)

Evr*_*sen 10

标记的答案是从这个博客的答案复制,没有学分:(

以下是代码如何计算它的解释...你可以看看DefaultRetryPolicy.java它毕竟是开源的......

初始化时,它使用用户给定的值:

    /**
     * Constructs a new retry policy.
     *
     * @param initialTimeoutMs The initial timeout for the policy.
     * @param maxNumRetries The maximum number of retries.
     * @param backoffMultiplier Backoff multiplier for the policy.
     */
    public DefaultRetryPolicy(int initialTimeoutMs, int maxNumRetries, float backoffMultiplier) {
        mCurrentTimeoutMs = initialTimeoutMs;
        mMaxNumRetries = maxNumRetries;
        mBackoffMultiplier = backoffMultiplier;
}
Run Code Online (Sandbox Code Playgroud)

因此,当代码检查当前超时时,它会收到initialTimeoutMs第一个请求的值(而不是重试请求,原始请求).

    /** Returns the current timeout. */
    @Override
    public int getCurrentTimeout() {
        return mCurrentTimeoutMs;
    }
Run Code Online (Sandbox Code Playgroud)

如果a retry()被调用则重新计算超时(对于retrt请求2nd,3rd等):

/**
 * Prepares for the next retry by applying a backoff to the timeout.
 *
 * @param error The error code of the last attempt.
 */
@Override
public void retry(VolleyError error) throws VolleyError {
    mCurrentRetryCount++;
    mCurrentTimeoutMs += (int) (mCurrentTimeoutMs * mBackoffMultiplier);
    if (!hasAttemptRemaining()) {
        throw error;
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,仅在第一个请求失败时,在第二个请求(重试请求)中添加后退乘数...依此类推......

initialTimeoutMs = 5000ms,maxNumRetries = 2,backoffMultiplier = 2

第1次请求超时5s第2次请求超时(重试)5 + 10 = 15s第3次请求超时(重试)15 + 30 = 45s

因此,将有1个正常请求+ 2个重试.由于代码,总共3个请求

/** Returns true if this policy has attempts remaining, false otherwise. */
protected boolean hasAttemptRemaining() {
    return mCurrentRetryCount <= mMaxNumRetries;
}
Run Code Online (Sandbox Code Playgroud)

在第一个请求mCurrentRetryCount为0之后,在第二个请求1之后,以及在第三个请求之后2.仅在第4个请求时它将返回false因为mCurrentRetryCount仅从retry()方法增加了.