Skip to content

Commit f284b72

Browse files
刘斌刘斌
authored andcommitted
分片与完成上传分开检查
1 parent a874c66 commit f284b72

File tree

9 files changed

+162
-33
lines changed

9 files changed

+162
-33
lines changed

library/src/androidTest/java/com/qiniu/android/FormUploadTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
5959
e.printStackTrace();
6060
}
6161
Assert.assertEquals(info.toString(), expectKey, key);
62-
Assert.assertTrue(info.toString(), info.isOK());
62+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
6363
Assert.assertNotNull(info.reqId);
6464
Assert.assertNotNull(resp);
6565

@@ -92,7 +92,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
9292
}
9393
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
9494
Assert.assertEquals(info.toString(), expectKey, key);
95-
Assert.assertFalse(info.toString(), info.isOK());
95+
Assert.assertFalse(info.toString(), ResponseInfo.isUpOK(info, resp));
9696
Assert.assertEquals(info.toString(), "", info.reqId);
9797
Assert.assertNull(resp);
9898
}
@@ -125,7 +125,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
125125
}
126126

127127
Assert.assertEquals(info.toString(), expectKey, key);
128-
Assert.assertTrue(info.toString(), info.isOK());
128+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
129129

130130
Assert.assertNotNull(info.reqId);
131131
Assert.assertNotNull(resp);
@@ -270,7 +270,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
270270
}
271271

272272
Assert.assertEquals(info.toString(), expectKey, key);
273-
Assert.assertTrue(info.toString(), info.isOK());
273+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
274274
//上传策略含空格 \"fname\":\" $(fname) \"
275275
Assert.assertEquals(f.getName(), resp.optString("fname", "res doesn't include the FNAME").trim());
276276
Assert.assertNotNull(info.reqId);
@@ -307,7 +307,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
307307
Assert.assertEquals(f.toString(), 0, f.length());
308308
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
309309
Assert.assertEquals(info.toString(), expectKey, key);
310-
Assert.assertFalse(info.toString(), info.isOK());
310+
Assert.assertFalse(info.toString(), ResponseInfo.isUpOK(info, resp));
311311
Assert.assertEquals(info.toString(), "", info.reqId);
312312
Assert.assertNull(resp);
313313
TempFile.remove(f);
@@ -369,7 +369,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
369369
}
370370

371371
Assert.assertEquals(info.toString(), expectKey, key);
372-
Assert.assertTrue(info.toString(), info.isOK());
372+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
373373
Assert.assertNotNull(info.reqId);
374374
Assert.assertNotNull(resp);
375375
}
@@ -406,7 +406,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
406406
}
407407

408408
Assert.assertEquals(info.toString(), expectKey, key);
409-
Assert.assertTrue(info.toString(), info.isOK());
409+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
410410
Assert.assertNotNull(info.reqId);
411411
Assert.assertNotNull(resp);
412412
}
@@ -443,7 +443,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
443443
}
444444

445445
Assert.assertEquals(info.toString(), expectKey, key);
446-
Assert.assertTrue(info.toString(), info.isOK());
446+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
447447
Assert.assertNotNull(info.reqId);
448448
Assert.assertNotNull(resp);
449449
}
@@ -479,7 +479,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
479479
e.printStackTrace();
480480
}
481481
Assert.assertEquals(info.toString(), expectKey, key);
482-
Assert.assertTrue(info.toString(), info.isOK());
482+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
483483
Assert.assertNotNull(info.reqId);
484484
Assert.assertNotNull(resp);
485485
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.qiniu.android;
2+
3+
import android.test.AndroidTestCase;
4+
5+
import junit.framework.Assert;
6+
7+
import org.json.JSONArray;
8+
import org.json.JSONException;
9+
import org.json.JSONObject;
10+
11+
/**
12+
* Created by Simon on 3/3/16.
13+
*/
14+
public class JsonTest extends AndroidTestCase {
15+
16+
private boolean showContent = false;
17+
18+
public void testEmpty() {
19+
JSONObject json = new JSONObject();
20+
Assert.assertNotNull(json);
21+
Assert.assertEquals("{}", json.toString());
22+
}
23+
24+
// e: org.json.JSONException: End of input at character 0 of
25+
public void testEmpty1() throws JSONException {
26+
String str = "";
27+
Exception ex = null;
28+
try {
29+
JSONObject json = new JSONObject(str);
30+
} catch (JSONException e) {
31+
ex = e;
32+
}
33+
Assert.assertNotNull(ex);
34+
if (showContent) {
35+
Assert.assertEquals(str, ex.getMessage());
36+
}
37+
}
38+
39+
//e: org.json.JSONException: End of input at character 2 of
40+
public void testEmpty2() throws JSONException {
41+
String str = " ";
42+
Exception ex = null;
43+
try {
44+
JSONObject json = new JSONObject(str);
45+
} catch (JSONException e) {
46+
ex = e;
47+
}
48+
Assert.assertNotNull(ex);
49+
if (showContent) {
50+
Assert.assertEquals(str, ex.getMessage());
51+
}
52+
}
53+
54+
public void testB() throws JSONException {
55+
String str = "{}";
56+
JSONObject json = new JSONObject(str);
57+
Assert.assertNotNull(json);
58+
if (showContent) {
59+
Assert.assertEquals(str, json.toString());
60+
}
61+
}
62+
63+
// e: org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject
64+
public void testArray() throws JSONException {
65+
String str = "[]";
66+
Exception ex = null;
67+
try {
68+
JSONObject json = new JSONObject(str);// should JSONArray
69+
} catch (JSONException e) {
70+
ex = e;
71+
}
72+
Assert.assertNotNull(ex);
73+
if (showContent) {
74+
Assert.assertEquals(str, ex.getMessage());
75+
}
76+
}
77+
78+
//e: org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject
79+
public void testNull() throws JSONException {
80+
String str = "null";
81+
Exception ex = null;
82+
try {
83+
JSONObject json = new JSONObject(str);
84+
} catch (JSONException e) {
85+
ex = e;
86+
}
87+
Assert.assertNotNull(ex);
88+
if (showContent) {
89+
Assert.assertEquals(str, ex.getMessage());
90+
}
91+
}
92+
}

library/src/androidTest/java/com/qiniu/android/PortTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private void check(final String expectKey) {
105105
}
106106

107107
Assert.assertEquals(info.toString(), expectKey, key);
108-
Assert.assertTrue(info.toString(), info.isOK());
108+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
109109
Assert.assertNotNull(info.reqId);
110110
Assert.assertNotNull(resp);
111111
}

library/src/androidTest/java/com/qiniu/android/ResumeUploadTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
6060

6161
Assert.assertEquals(info.toString(), expectKey, key);
6262

63-
Assert.assertTrue(info.toString(), info.isOK());
63+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
6464

6565
Assert.assertNotNull(info.reqId);
6666
Assert.assertNotNull(resp);
@@ -97,13 +97,13 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
9797

9898
Assert.assertEquals(info.toString(), expectKey, key);
9999

100-
Assert.assertTrue(info.toString(), info.isOK());
100+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
101101

102102
Assert.assertEquals(expectKey, key);
103103

104104
//上传策略含空格 \"fname\":\" $(fname) \"
105105
Assert.assertEquals(f.getName(), resp.optString("fname", "res doesn't include the FNAME").trim());
106-
Assert.assertTrue(info.isOK());
106+
Assert.assertTrue(ResponseInfo.isUpOK(info, resp));
107107
Assert.assertNotNull(info.reqId);
108108
Assert.assertNotNull(resp);
109109
String hash = resp.getString("hash");
@@ -141,10 +141,10 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
141141

142142
Assert.assertEquals(info.toString(), expectKey, key);
143143

144-
Assert.assertTrue(info.toString(), info.isOK());
144+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
145145

146146
Assert.assertEquals(expectKey, key);
147-
Assert.assertTrue(info.isOK());
147+
Assert.assertTrue(ResponseInfo.isUpOK(info, resp));
148148
Assert.assertNotNull(info.reqId);
149149
Assert.assertNotNull(resp);
150150
TempFile.remove(f);

library/src/androidTest/java/com/qiniu/android/TestFileRecorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
127127
}
128128

129129
Assert.assertEquals(info.toString(), expectKey, key);
130-
Assert.assertTrue(info.toString(), info.isOK());
130+
Assert.assertTrue(info.toString(), ResponseInfo.isUpOK(info, resp));
131131
Assert.assertTrue(!failed);
132132
Assert.assertNotNull(resp);
133133

library/src/main/java/com/qiniu/android/http/Client.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.qiniu.android.storage.UpCancellationSignal;
77
import com.qiniu.android.utils.AsyncRun;
88
import com.qiniu.android.utils.StringMap;
9+
import com.qiniu.android.utils.StringUtils;
910
import com.squareup.okhttp.Callback;
1011
import com.squareup.okhttp.Dns;
1112
import com.squareup.okhttp.Interceptor;
@@ -119,6 +120,10 @@ private static String ctype(com.squareup.okhttp.Response response) {
119120

120121
private static JSONObject buildJsonResp(byte[] body) throws Exception {
121122
String str = new String(body, Constants.UTF_8);
123+
// 允许 空 字符串
124+
if (StringUtils.isNullOrEmpty(str)) {
125+
return new JSONObject();
126+
}
122127
return new JSONObject(str);
123128
}
124129

library/src/main/java/com/qiniu/android/http/ResponseInfo.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
import com.qiniu.android.common.Constants;
55

6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
69
import java.util.Locale;
710

811
/**
@@ -124,9 +127,6 @@ public boolean isCancelled() {
124127
return statusCode == Cancelled;
125128
}
126129

127-
public boolean isOK() {
128-
return statusCode == 200 && error == null && reqId != null;
129-
}
130130

131131
public boolean isNetworkBroken() {
132132
return statusCode == NetworkError || statusCode == UnknownHost
@@ -148,12 +148,45 @@ public boolean needRetry() {
148148
|| (statusCode == 200 && error != null));
149149
}
150150

151-
public boolean isNotQiniu() {
152-
return statusCode < 500 && statusCode >= 200 && reqId == null;
153-
}
154151

155152
public String toString() {
156153
return String.format(Locale.ENGLISH, "{ver:%s,ResponseInfo:%s,status:%d, reqId:%s, xlog:%s, xvia:%s, host:%s, path:%s, ip:%s, port:%d, duration:%f s, time:%d, sent:%d,error:%s}",
157154
Constants.VERSION, id, statusCode, reqId, xlog, xvia, host, path, ip, port, duration, timeStamp, sent, error);
158155
}
156+
157+
public boolean hasReqId() {
158+
return reqId != null;
159+
}
160+
161+
162+
163+
/**
164+
* 文件上传执行结束时检查是否正常完成上传
165+
* */
166+
public static boolean isUpOK(ResponseInfo info, JSONObject response) {
167+
return info.statusCode == 200 && info.error == null && (info.hasReqId() || response != null);
168+
}
169+
170+
public static boolean isChunkOK(ResponseInfo info, JSONObject response) {
171+
return info.statusCode == 200 && info.error == null && (info.hasReqId() || isChunkResOK(response));
172+
}
173+
174+
private static boolean isChunkResOK(JSONObject response) {
175+
try {
176+
// getXxxx 若获取不到值,会抛出异常
177+
response.getString("ctx");
178+
response.getLong("crc32");
179+
} catch (Exception e) {
180+
return false;
181+
}
182+
return true;
183+
}
184+
185+
public static boolean isNotUpToQiniu(ResponseInfo info, JSONObject response) {
186+
return info.statusCode < 500 && info.statusCode >= 200 && (info.hasReqId() || response != null);
187+
}
188+
189+
public static boolean isNotChunkToQiniu(ResponseInfo info, JSONObject response) {
190+
return info.statusCode < 500 && info.statusCode >= 200 && (info.hasReqId() || isChunkResOK(response));
191+
}
159192
}

library/src/main/java/com/qiniu/android/storage/FormUploader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,24 @@ public void onProgress(int bytesWritten, int totalSize) {
108108
CompletionHandler completion = new CompletionHandler() {
109109
@Override
110110
public void complete(ResponseInfo info, JSONObject response) {
111-
if (info.isOK()) {
111+
if (ResponseInfo.isUpOK(info, response)) {
112112
options.progressHandler.progress(key, 1.0);
113113
completionHandler.complete(key, info, response);
114114
} else if (options.cancellationSignal.isCancelled()) {
115115
ResponseInfo i = ResponseInfo.cancelled();
116116
completionHandler.complete(key, i, null);
117-
} else if (info.needRetry() || (info.isNotQiniu() && !token.hasReturnUrl())) {
117+
} else if (info.needRetry() || (ResponseInfo.isNotUpToQiniu(info, response) && !token.hasReturnUrl())) {
118118
CompletionHandler retried = new CompletionHandler() {
119119
@Override
120120
public void complete(ResponseInfo info, JSONObject response) {
121-
if (info.isOK()) {
121+
if (ResponseInfo.isUpOK(info, response)) {
122122
options.progressHandler.progress(key, 1.0);
123123
}
124124
completionHandler.complete(key, info, response);
125125
}
126126
};
127127
URI u = config.up.address;
128-
if (info.needSwitchServer() || info.isNotQiniu()) {
128+
if (info.needSwitchServer() || ResponseInfo.isNotUpToQiniu(info, response)) {
129129
u = config.upBackup.address;
130130
}
131131

library/src/main/java/com/qiniu/android/storage/ResumeUploader.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,18 @@ private void nextTask(final int offset, final int retried, final URI address) {
198198
}
199199

200200
if (offset == size) {
201+
//完成操作,返回的内容不确定,是否真正成功逻辑让用户自己判断
201202
CompletionHandler complete = new CompletionHandler() {
202203
@Override
203204
public void complete(ResponseInfo info, JSONObject response) {
204-
if (info.isOK()) {
205+
if (ResponseInfo.isUpOK(info, response)) {
205206
removeRecord();
206207
options.progressHandler.progress(key, 1.0);
207208
completionHandler.complete(key, info, response);
208209
return;
209210
}
210211

211-
if ((isNotQiniu(info) || info.needRetry()) && retried < config.retryMax) {
212+
if ((ResponseInfo.isNotUpToQiniu(info, response) && !token.hasReturnUrl() || info.needRetry()) && retried < config.retryMax) {
212213
nextTask(offset, retried + 1, config.upBackup.address);
213214
return;
214215
}
@@ -231,16 +232,17 @@ public void onProgress(int bytesWritten, int totalSize) {
231232
}
232233
};
233234

235+
// 分片上传,七牛响应内容固定,若缺少reqId,可通过响应体判断
234236
CompletionHandler complete = new CompletionHandler() {
235237
@Override
236238
public void complete(ResponseInfo info, JSONObject response) {
237-
if (!info.isOK()) {
239+
if (!ResponseInfo.isChunkOK(info, response)) {
238240
if (info.statusCode == 701 && retried < config.retryMax) {
239241
nextTask((offset / Configuration.BLOCK_SIZE) * Configuration.BLOCK_SIZE, retried + 1, address);
240242
return;
241243
}
242244

243-
if ((isNotQiniu(info) || info.needRetry()) && retried < config.retryMax) {
245+
if ((ResponseInfo.isNotChunkToQiniu(info, response) || info.needRetry()) && retried < config.retryMax) {
244246
nextTask(offset, retried + 1, config.upBackup.address);
245247
return;
246248
}
@@ -331,9 +333,6 @@ private void record(int offset) {
331333
config.recorder.set(recorderKey, data.getBytes());
332334
}
333335

334-
private boolean isNotQiniu(ResponseInfo info) {
335-
return info.isNotQiniu() && !token.hasReturnUrl();
336-
}
337336

338337
private URI newURI(URI uri, String path) {
339338
try {

0 commit comments

Comments
 (0)