/**
* 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;
}
}
如有理解错误 望指出。。
android初学------ volley学习-重试请求策略
最新推荐文章于 2024-05-22 09:55:51 发布