From af4a8dbf64a5eb1b3e9bccd108ddec381d75c28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=96=8C?= Date: Fri, 22 Jan 2016 15:38:18 +0800 Subject: [PATCH] retry --- .../qiniu/android/storage/ResumeUploader.java | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/library/src/main/java/com/qiniu/android/storage/ResumeUploader.java b/library/src/main/java/com/qiniu/android/storage/ResumeUploader.java index 216ad4712..ad562981a 100644 --- a/library/src/main/java/com/qiniu/android/storage/ResumeUploader.java +++ b/library/src/main/java/com/qiniu/android/storage/ResumeUploader.java @@ -181,6 +181,12 @@ private boolean isCancelled() { } private void nextTask(final int offset, final int retried, final String host) { + if (isCancelled()) { + ResponseInfo i = ResponseInfo.cancelled(); + completionHandler.complete(key, i, null); + return; + } + if (offset == size) { CompletionHandler complete = new CompletionHandler() { @Override @@ -192,20 +198,20 @@ public void complete(ResponseInfo info, JSONObject response) { return; } - if (isCancelled()) { - ResponseInfo i = ResponseInfo.cancelled(); - completionHandler.complete(key, i, null); - return; - } - if (isNotQiniu(info)) { forceIp = true; } - if (isNotQiniu(info) || (info.needRetry() && retried < config.retryMax)) { - nextTask(offset, retried + 1, host); + String host2 = host; + if (info.needSwitchServer()) { + host2 = config.upHostBackup; + } + + if ((isNotQiniu(info) || info.needRetry()) && retried < config.retryMax) { + nextTask(offset, retried + 1, host2); return; } + completionHandler.complete(key, info, response); } }; @@ -229,35 +235,36 @@ public void onProgress(int bytesWritten, int totalSize) { @Override public void complete(ResponseInfo info, JSONObject response) { if (!info.isOK()) { - if (isCancelled()) { - ResponseInfo i = ResponseInfo.cancelled(); - completionHandler.complete(key, i, null); - return; - } - if (info.statusCode == 701) { - nextTask((offset / Configuration.BLOCK_SIZE) * Configuration.BLOCK_SIZE, retried, host); + if (info.statusCode == 701 && retried < config.retryMax) { + nextTask((offset / Configuration.BLOCK_SIZE) * Configuration.BLOCK_SIZE, retried + 1, host); return; } if (isNotQiniu(info)) { forceIp = true; } - if (!isNotQiniu(info) && (retried >= config.retryMax || !info.needRetry())) { - completionHandler.complete(key, info, null); - return; - } + String host2 = host; if (info.needSwitchServer()) { host2 = config.upHostBackup; } - nextTask(offset, retried + 1, host2); + + if ((isNotQiniu(info) || info.needRetry()) && retried < config.retryMax) { + nextTask(offset, retried + 1, host2); + return; + } + + completionHandler.complete(key, info, null); return; } - String context = null; - if (response == null) { + // info.isOK() + + if (response == null && retried < config.retryMax) { nextTask(offset, retried + 1, host); return; } + + String context = null; long crc = 0; try { context = response.getString("ctx"); @@ -265,7 +272,7 @@ public void complete(ResponseInfo info, JSONObject response) { } catch (JSONException e) { e.printStackTrace(); } - if (context == null || crc != ResumeUploader.this.crc32) { + if ((context == null || crc != ResumeUploader.this.crc32) && retried < config.retryMax) { nextTask(offset, retried + 1, host); return; }