Skip to content
Merged

retry #184

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions library/src/main/java/com/qiniu/android/storage/ResumeUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
};
Expand All @@ -229,43 +235,44 @@ 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");
crc = response.getLong("crc32");
} 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;
}
Expand Down