Skip to content

Commit 713df49

Browse files
committed
Merge pull request #191 from sxci/7.1_up_success_check
7.1 up success check
2 parents a874c66 + 7d28d29 commit 713df49

File tree

5 files changed

+147
-20
lines changed

5 files changed

+147
-20
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void testPost1() throws Throwable {
4747
"hello".getBytes(), null, null, new CompletionHandler() {
4848
@Override
4949
public void complete(ResponseInfo rinfo, JSONObject response) {
50+
Assert.assertNotNull(rinfo);
5051
Log.d("qiniutest", rinfo.toString());
5152
info = rinfo;
5253
signal.countDown();
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/main/java/com/qiniu/android/http/Client.java

Lines changed: 8 additions & 3 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

@@ -167,7 +172,7 @@ public void onFailure(Request request, IOException e) {
167172
}
168173

169174
URL u = request.url();
170-
ResponseInfo info = new ResponseInfo(statusCode, "", "", "", u.getHost(), u.getPath(), "", u.getPort(), duration, 0, e.getMessage());
175+
ResponseInfo info = new ResponseInfo(null, statusCode, "", "", "", u.getHost(), u.getPath(), "", u.getPort(), duration, 0, e.getMessage());
171176

172177
complete.complete(info, null);
173178
}
@@ -281,8 +286,8 @@ private void onRet(com.squareup.okhttp.Response response, String ip, long durati
281286
}
282287

283288
URL u = response.request().url();
284-
final ResponseInfo info = new ResponseInfo(code, reqId, response.header("X-Log"), via(response),
285-
u.getHost(), u.getPath(), ip, u.getPort(), duration, 0, error);
289+
final ResponseInfo info = new ResponseInfo(json, code, reqId, response.header("X-Log"),
290+
via(response), u.getHost(), u.getPath(), ip, u.getPort(), duration, 0, error);
286291
final JSONObject json2 = json;
287292
AsyncRun.run(new Runnable() {
288293
@Override

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

Lines changed: 20 additions & 11 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
/**
@@ -81,7 +84,11 @@ public final class ResponseInfo {
8184
*/
8285
public final long sent;
8386

84-
public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, String host, String path, String ip, int port, double duration, long sent, String error) {
87+
private final JSONObject response;
88+
89+
public ResponseInfo(JSONObject json, int statusCode, String reqId, String xlog, String xvia, String host,
90+
String path, String ip, int port, double duration, long sent, String error) {
91+
response = json;
8592
this.statusCode = statusCode;
8693
this.reqId = reqId;
8794
this.xlog = xlog;
@@ -98,34 +105,31 @@ public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, Stri
98105
}
99106

100107
public static ResponseInfo zeroSize() {
101-
return new ResponseInfo(ZeroSizeFile, "", "", "", "", "", "", -1, 0, 0, "file or data size is zero");
108+
return new ResponseInfo(null, ZeroSizeFile, "", "", "", "", "", "", -1, 0, 0, "file or data size is zero");
102109
}
103110

104111
public static ResponseInfo cancelled() {
105-
return new ResponseInfo(Cancelled, "", "", "", "", "", "", -1, 0, 0, "cancelled by user");
112+
return new ResponseInfo(null, Cancelled, "", "", "", "", "", "", -1, 0, 0, "cancelled by user");
106113
}
107114

108115
public static ResponseInfo invalidArgument(String message) {
109-
return new ResponseInfo(InvalidArgument, "", "", "", "", "", "", -1, 0, 0,
110-
message);
116+
return new ResponseInfo(null, InvalidArgument, "", "", "", "", "", "", -1, 0, 0, message);
111117
}
112118

113119
public static ResponseInfo invalidToken(String message) {
114-
return new ResponseInfo(InvalidToken, "", "", "", "", "", "", -1, 0, 0,
115-
message);
120+
return new ResponseInfo(null, InvalidToken, "", "", "", "", "", "", -1, 0, 0, message);
116121
}
117122

118123
public static ResponseInfo fileError(Exception e) {
119-
return new ResponseInfo(InvalidFile, "", "", "", "", "", "", -1,
120-
0, 0, e.getMessage());
124+
return new ResponseInfo(null, InvalidFile, "", "", "", "", "", "", -1, 0, 0, e.getMessage());
121125
}
122126

123127
public boolean isCancelled() {
124128
return statusCode == Cancelled;
125129
}
126130

127131
public boolean isOK() {
128-
return statusCode == 200 && error == null && reqId != null;
132+
return statusCode == 200 && error == null && (hasReqId() || response != null);
129133
}
130134

131135
public boolean isNetworkBroken() {
@@ -149,11 +153,16 @@ public boolean needRetry() {
149153
}
150154

151155
public boolean isNotQiniu() {
152-
return statusCode < 500 && statusCode >= 200 && reqId == null;
156+
return statusCode < 500 && statusCode >= 200 && (!hasReqId() && response == null);
153157
}
154158

155159
public String toString() {
156160
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}",
157161
Constants.VERSION, id, statusCode, reqId, xlog, xvia, host, path, ip, port, duration, timeStamp, sent, error);
158162
}
163+
164+
public boolean hasReqId() {
165+
return reqId != null;
166+
}
167+
159168
}

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ 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) {
@@ -208,7 +209,7 @@ public void complete(ResponseInfo info, JSONObject response) {
208209
return;
209210
}
210211

211-
if ((isNotQiniu(info) || info.needRetry()) && retried < config.retryMax) {
212+
if ((info.isNotQiniu() && !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 (!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 ((isNotChunkToQiniu(info, response) || info.needRetry()) && retried < config.retryMax) {
244246
nextTask(offset, retried + 1, config.upBackup.address);
245247
return;
246248
}
@@ -279,6 +281,27 @@ public void complete(ResponseInfo info, JSONObject response) {
279281
putChunk(address, offset, chunkSize, context, progress, complete, options.cancellationSignal);
280282
}
281283

284+
285+
private static boolean isChunkOK(ResponseInfo info, JSONObject response) {
286+
return info.statusCode == 200 && info.error == null && (info.hasReqId() || isChunkResOK(response));
287+
}
288+
289+
private static boolean isChunkResOK(JSONObject response) {
290+
try {
291+
// getXxxx 若获取不到值,会抛出异常
292+
response.getString("ctx");
293+
response.getLong("crc32");
294+
} catch (Exception e) {
295+
return false;
296+
}
297+
return true;
298+
}
299+
300+
301+
private static boolean isNotChunkToQiniu(ResponseInfo info, JSONObject response) {
302+
return info.statusCode < 500 && info.statusCode >= 200 && (!info.hasReqId() && !isChunkResOK(response));
303+
}
304+
282305
private int recoveryFromRecord() {
283306
if (config.recorder == null) {
284307
return 0;
@@ -331,9 +354,6 @@ private void record(int offset) {
331354
config.recorder.set(recorderKey, data.getBytes());
332355
}
333356

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

338358
private URI newURI(URI uri, String path) {
339359
try {

0 commit comments

Comments
 (0)