If you think the Android project volley listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2011 The Android Open Source Project
*//fromwww.java2s.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.android.volley;
/**
* Default retry policy for requests.
*/publicclass DefaultRetryPolicy implements RetryPolicy {
/** The current timeout in milliseconds. */privateint mCurrentTimeoutMs;
/** The current retry count. */privateint mCurrentRetryCount;
/** The maximum number of attempts. */privatefinalint mMaxNumRetries;
/** The backoff multiplier for for the policy. */privatefinalfloat mBackoffMultiplier;
/** The default socket timeout in milliseconds */publicstaticfinalint DEFAULT_TIMEOUT_MS = 5000;
/** The default number of retries */publicstaticfinalint DEFAULT_MAX_RETRIES = 2;
/** The default backoff multiplier */publicstaticfinalfloat DEFAULT_BACKOFF_MULT = 1.2f;
/**
* 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
publicint getCurrentTimeout() {
return mCurrentTimeoutMs;
}
/**
* Returns the current retry count.
*/
@Override
publicint getCurrentRetryCount() {
return mCurrentRetryCount;
}
/**
* Prepares for the next retry by applying a backoff to the timeout.
* @param error The error code of the last attempt.
*/
@Override
publicvoid retry(VolleyError error) throws VolleyError {
mCurrentRetryCount++;
mCurrentTimeoutMs += (mCurrentTimeoutMs * mBackoffMultiplier);
if (!hasAttemptRemaining()) {
throw error;
}
}
/**
* Returns true if this policy has attempts remaining, false otherwise.
*/protectedboolean hasAttemptRemaining() {
return mCurrentRetryCount <= mMaxNumRetries;
}
}