Skip to content

Commit acc278b

Browse files
刘斌刘斌
authored andcommitted
维持isOK isNotQiniu 方法不变
1 parent 2269177 commit acc278b

File tree

8 files changed

+64
-67
lines changed

8 files changed

+64
-67
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(), ResponseInfo.isUpOK(info, resp));
62+
Assert.assertTrue(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
95+
Assert.assertFalse(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
128+
Assert.assertTrue(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
273+
Assert.assertTrue(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
310+
Assert.assertFalse(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
372+
Assert.assertTrue(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
409+
Assert.assertTrue(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
446+
Assert.assertTrue(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
482+
Assert.assertTrue(info.toString(), info.isOK());
483483
Assert.assertNotNull(info.reqId);
484484
Assert.assertNotNull(resp);
485485
}

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(), ResponseInfo.isUpOK(info, resp));
108+
Assert.assertTrue(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
63+
Assert.assertTrue(info.toString(), info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
100+
Assert.assertTrue(info.toString(), info.isOK());
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(ResponseInfo.isUpOK(info, resp));
106+
Assert.assertTrue(info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
144+
Assert.assertTrue(info.toString(), info.isOK());
145145

146146
Assert.assertEquals(expectKey, key);
147-
Assert.assertTrue(ResponseInfo.isUpOK(info, resp));
147+
Assert.assertTrue(info.isOK());
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(), ResponseInfo.isUpOK(info, resp));
130+
Assert.assertTrue(info.toString(), info.isOK());
131131
Assert.assertTrue(!failed);
132132
Assert.assertNotNull(resp);
133133

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public void onFailure(Request request, IOException e) {
172172
}
173173

174174
URL u = request.url();
175-
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());
176176

177177
complete.complete(info, null);
178178
}
@@ -286,8 +286,8 @@ private void onRet(com.squareup.okhttp.Response response, String ip, long durati
286286
}
287287

288288
URL u = response.request().url();
289-
final ResponseInfo info = new ResponseInfo(code, reqId, response.header("X-Log"), via(response),
290-
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);
291291
final JSONObject json2 = json;
292292
AsyncRun.run(new Runnable() {
293293
@Override

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

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ public final class ResponseInfo {
8484
*/
8585
public final long sent;
8686

87-
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;
8892
this.statusCode = statusCode;
8993
this.reqId = reqId;
9094
this.xlog = xlog;
@@ -101,32 +105,32 @@ public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, Stri
101105
}
102106

103107
public static ResponseInfo zeroSize() {
104-
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");
105109
}
106110

107111
public static ResponseInfo cancelled() {
108-
return new ResponseInfo(Cancelled, "", "", "", "", "", "", -1, 0, 0, "cancelled by user");
112+
return new ResponseInfo(null, Cancelled, "", "", "", "", "", "", -1, 0, 0, "cancelled by user");
109113
}
110114

111115
public static ResponseInfo invalidArgument(String message) {
112-
return new ResponseInfo(InvalidArgument, "", "", "", "", "", "", -1, 0, 0,
113-
message);
116+
return new ResponseInfo(null, InvalidArgument, "", "", "", "", "", "", -1, 0, 0, message);
114117
}
115118

116119
public static ResponseInfo invalidToken(String message) {
117-
return new ResponseInfo(InvalidToken, "", "", "", "", "", "", -1, 0, 0,
118-
message);
120+
return new ResponseInfo(null, InvalidToken, "", "", "", "", "", "", -1, 0, 0, message);
119121
}
120122

121123
public static ResponseInfo fileError(Exception e) {
122-
return new ResponseInfo(InvalidFile, "", "", "", "", "", "", -1,
123-
0, 0, e.getMessage());
124+
return new ResponseInfo(null, InvalidFile, "", "", "", "", "", "", -1, 0, 0, e.getMessage());
124125
}
125126

126127
public boolean isCancelled() {
127128
return statusCode == Cancelled;
128129
}
129130

131+
public boolean isOK() {
132+
return statusCode == 200 && error == null && (hasReqId() || response != null);
133+
}
130134

131135
public boolean isNetworkBroken() {
132136
return statusCode == NetworkError || statusCode == UnknownHost
@@ -148,6 +152,9 @@ public boolean needRetry() {
148152
|| (statusCode == 200 && error != null));
149153
}
150154

155+
public boolean isNotQiniu() {
156+
return statusCode < 500 && statusCode >= 200 && (!hasReqId() && response == null);
157+
}
151158

152159
public String toString() {
153160
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}",
@@ -158,35 +165,4 @@ public boolean hasReqId() {
158165
return reqId != null;
159166
}
160167

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-
}
192168
}

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 (ResponseInfo.isUpOK(info, response)) {
111+
if (info.isOK()) {
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() || (ResponseInfo.isNotUpToQiniu(info, response) && !token.hasReturnUrl())) {
117+
} else if (info.needRetry() || (info.isNotQiniu() && !token.hasReturnUrl())) {
118118
CompletionHandler retried = new CompletionHandler() {
119119
@Override
120120
public void complete(ResponseInfo info, JSONObject response) {
121-
if (ResponseInfo.isUpOK(info, response)) {
121+
if (info.isOK()) {
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() || ResponseInfo.isNotUpToQiniu(info, response)) {
128+
if (info.needSwitchServer() || info.isNotQiniu()) {
129129
u = config.upBackup.address;
130130
}
131131

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,14 @@ private void nextTask(final int offset, final int retried, final URI address) {
202202
CompletionHandler complete = new CompletionHandler() {
203203
@Override
204204
public void complete(ResponseInfo info, JSONObject response) {
205-
if (ResponseInfo.isUpOK(info, response)) {
205+
if (info.isOK()) {
206206
removeRecord();
207207
options.progressHandler.progress(key, 1.0);
208208
completionHandler.complete(key, info, response);
209209
return;
210210
}
211211

212-
if ((ResponseInfo.isNotUpToQiniu(info, response) && !token.hasReturnUrl() || info.needRetry()) && retried < config.retryMax) {
212+
if ((info.isNotQiniu() && !token.hasReturnUrl() || info.needRetry()) && retried < config.retryMax) {
213213
nextTask(offset, retried + 1, config.upBackup.address);
214214
return;
215215
}
@@ -236,13 +236,13 @@ public void onProgress(int bytesWritten, int totalSize) {
236236
CompletionHandler complete = new CompletionHandler() {
237237
@Override
238238
public void complete(ResponseInfo info, JSONObject response) {
239-
if (!ResponseInfo.isChunkOK(info, response)) {
239+
if (!isChunkOK(info, response)) {
240240
if (info.statusCode == 701 && retried < config.retryMax) {
241241
nextTask((offset / Configuration.BLOCK_SIZE) * Configuration.BLOCK_SIZE, retried + 1, address);
242242
return;
243243
}
244244

245-
if ((ResponseInfo.isNotChunkToQiniu(info, response) || info.needRetry()) && retried < config.retryMax) {
245+
if ((isNotChunkToQiniu(info, response) || info.needRetry()) && retried < config.retryMax) {
246246
nextTask(offset, retried + 1, config.upBackup.address);
247247
return;
248248
}
@@ -281,6 +281,27 @@ public void complete(ResponseInfo info, JSONObject response) {
281281
putChunk(address, offset, chunkSize, context, progress, complete, options.cancellationSignal);
282282
}
283283

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+
284305
private int recoveryFromRecord() {
285306
if (config.recorder == null) {
286307
return 0;

0 commit comments

Comments
 (0)