Created
February 18, 2014 00:57
-
-
Save Kuchinashi/9062470 to your computer and use it in GitHub Desktop.
【Android Volley】RetryPolicyを適用するとリトライがキャンセルされなかった ref: http://qiita.com/kuchinashi_r/items/e664c17ef2144b5f8148
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public NetworkResponse performRequest(Request<?> request) throws VolleyError { | |
long requestStart = SystemClock.elapsedRealtime(); | |
while (true) { | |
HttpResponse httpResponse = null; | |
byte[] responseContents = null; | |
Map<String, String> responseHeaders = new HashMap<String, String>(); | |
try { | |
// 略 | |
} catch (SocketTimeoutException e) { | |
attemptRetryOnException("socket", request, new TimeoutError()); | |
} catch (ConnectTimeoutException e) { | |
attemptRetryOnException("connection", request, new TimeoutError()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Attempts to prepare the request for a retry. If there are no more attempts remaining in the | |
* request's retry policy, a timeout exception is thrown. | |
* @param request The request to use. | |
*/ | |
private static void attemptRetryOnException(String logPrefix, Request<?> request, | |
VolleyError exception) throws VolleyError { | |
RetryPolicy retryPolicy = request.getRetryPolicy(); | |
int oldTimeout = request.getTimeoutMs(); | |
try { | |
retryPolicy.retry(exception); | |
} catch (VolleyError e) { | |
request.addMarker( | |
String.format("%s-timeout-giveup [timeout=%s]", logPrefix, oldTimeout)); | |
throw e; | |
} | |
request.addMarker(String.format("%s-retry [timeout=%s]", logPrefix, oldTimeout)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (request.isCanceled()) { | |
request.finish("network-discard-cancelled"); | |
continue; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Request<?> mRequest; | |
public ExRetryPolicy(Request<?> request) { | |
super(CONNECTION_TIMEOUT, CONNECTION_RETRY_COUNT, 1f); | |
mRequest = request; | |
} | |
@Override | |
public void retry(VolleyError error) throws VolleyError { | |
NetworkResponse response = error.networkResponse; | |
if (response != null && response.statusCode >= 500 && response.statusCode < 600) { | |
// サーバーエラー時はリトライしない | |
throw error; | |
} | |
if(mRequest != null && mRequest.isCanceled()) { | |
// キャンセル済みならリトライしない(エラー処理されない) | |
throw error; | |
} | |
if (mInterval > 0) { | |
try { | |
Thread.sleep(mInterval); | |
} catch (InterruptedException e) { | |
} | |
} | |
VolleyLog.d("Network Retry count : %d", getCurrentRetryCount()); | |
super.retry(error); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (mRequest.isCanceled()) { | |
mRequest.finish("canceled-at-delivery"); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment