android初学------ volley学习-重试请求策略

/**
 * Default retry policy for requests.   重试连接 类
 */
public class DefaultRetryPolicy implements RetryPolicy {
    /** The current timeout in milliseconds. 
     * 当前设置的超时时间
     * */
    private int mCurrentTimeoutMs;

    /** The current retry count. 
     * 当前当前已重试次数
     * */
    private int mCurrentRetryCount;

    /** The maximum number of attempts.
     * 最多重试次数
     *  */
    private final int mMaxNumRetries;

    /** The backoff multiplier for for the policy.
     * <br />
     * 对于请求失败之后的请求,并不会隔相同的时间去请求Server,不会以线性的时间增长去请求,
     * 而是一个曲线增长,一次比一次长,如果backoff因子是2,当前超时为3,即下次再请求隔6S。
     *  */
    private final float mBackoffMultiplier;

    /** 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 <br />
     * 对于请求失败之后的请求,并不会隔相同的时间去请求Server,不会以线性的时间增长去请求,
     * 而是一个曲线增长,一次比一次长,如果backoff因子是2,当前超时为3,即下次再请求隔6S。
     * */
    public static final float DEFAULT_BACKOFF_MULT = 1f;

    /**
     * Constructs a new retry policy using the default timeouts.
     * 发起重试的请求策略  默认
     */
    public DefaultRetryPolicy() {
        this(DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT);
    }

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

    /**
     * Returns the current timeout.   得到当前设置的超时时间
     */
    @Override
    public int getCurrentTimeout() {
        return mCurrentTimeoutMs;
    }

    /**
     * Returns the current retry count.  得到当前重试的次数
     */
    @Override
    public int getCurrentRetryCount() {
        return mCurrentRetryCount;
    }

    /**
     * Prepares for the next retry by applying a backoff to the timeout.
     * <br />
     * 准备下一次连接的时间
     * @param error The error code of the last attempt.  最后尝试错误代码
     */
    @Override
    public void retry(VolleyError error) throws VolleyError {
        mCurrentRetryCount++;
        //当前超时时间 =当前超时时间+(当前超时时间 * 因子数 ) 比如 当前超时时间为 mCurrentTimeoutMs= 2500+(2500 * 1.0)
        //这样设置了下次超时的时间为5s之后超时
        mCurrentTimeoutMs += (mCurrentTimeoutMs * mBackoffMultiplier);
        //判断是否还有剩余次数
        if (!hasAttemptRemaining()) {
            throw error;
        }
    }

    /**
     * Returns true if this policy has attempts remaining, false otherwise.
     * <br />
     * 判断当前重复次数  和最大重复次数  如果当前重复次数 小于最大重复次数 就返回true  否则 返回false
     */
    protected boolean hasAttemptRemaining() {
        return mCurrentRetryCount <= mMaxNumRetries;
    }
}
如有理解错误 望指出。。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值