Skip to content

Commit b5114c1

Browse files
committed
合并,ResponseInfo创建方式一致
1 parent dc1d7a6 commit b5114c1

File tree

8 files changed

+71
-72
lines changed

8 files changed

+71
-72
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44
import android.util.Log;
55

66
import com.qiniu.android.collect.Config;
7-
import com.qiniu.android.storage.UploadManager;
8-
import com.qiniu.android.storage.persistent.FileRecorder;
9-
import com.qiniu.android.utils.UrlSafeBase64;
107

118
import junit.framework.Assert;
129

1310
import java.io.BufferedReader;
1411
import java.io.File;
1512
import java.io.FileReader;
1613
import java.io.IOException;
17-
import java.io.UnsupportedEncodingException;
18-
import java.util.ArrayList;
1914
import java.util.Date;
2015
import java.util.Iterator;
2116
import java.util.LinkedList;
@@ -25,10 +20,15 @@
2520
* Created by Simon on 12/6/16.
2621
*/
2722

28-
public class ACollectUploadInfoTest extends AndroidTestCase {
23+
public class ACollectUploadInfoTest extends AndroidTestCase {
2924

3025
private static long recordFileLastModified = 337l;
31-
private static Queue<Long> queue = new LinkedList<Long>(){{ offer(1l); offer(2l); offer(3l); offer(4l);}};
26+
private static Queue<Long> queue = new LinkedList<Long>() {{
27+
offer(1l);
28+
offer(2l);
29+
offer(3l);
30+
offer(4l);
31+
}};
3232

3333
@Override
3434
protected void setUp() throws Exception {
@@ -75,7 +75,7 @@ private static long getRecordFileAvgSize() {
7575
long sum = 0;
7676
long count = 0;
7777
Iterator<Long> it = queue.iterator();
78-
while(it.hasNext()) {
78+
while (it.hasNext()) {
7979
count += 1;
8080
sum += it.next();
8181
}
@@ -86,7 +86,7 @@ private static boolean isRecordFileSizeAllSame() {
8686
long size = -1;
8787
boolean same = true;
8888
Iterator<Long> it = queue.iterator();
89-
while(it.hasNext()) {
89+
while (it.hasNext()) {
9090
long temp = it.next();
9191
if (size > 0) {
9292
same = same && (size == temp);
@@ -99,7 +99,7 @@ private static boolean isRecordFileSizeAllSame() {
9999
private static String getRecordFileSizes() {
100100
StringBuilder ss = new StringBuilder("");
101101
Iterator<Long> it = queue.iterator();
102-
while(it.hasNext()) {
102+
while (it.hasNext()) {
103103
ss.append(it.next()).append(", ");
104104
}
105105
return ss.toString();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ public void test0File() throws Throwable {
172172
}
173173

174174
@SmallTest
175-
public void testNoComplete() {
176-
info = uploadManager.syncPut(new byte[0], null, null, null);
175+
public void test0byte() {
176+
info = uploadManager.syncPut(new byte[0], null, TestConfig.token, null);
177177
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
178178

179-
info = uploadManager.syncPut("", null, null, null);
179+
info = uploadManager.syncPut("", null, TestConfig.token, null);
180180
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
181181
}
182182

library/src/main/java/com/qiniu/android/collect/Config.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@
88
public class Config {
99
/**
1010
* 是否记录上传状态信息。 true 表示记录,false 表示不记录。
11-
*
11+
* <p>
1212
* 记录上传信息条件:
1313
* isRecord 为 true,
1414
* 记录文件大小 小于 maxRecordFileSize .
15-
*
15+
* <p>
1616
* 记录文件大小 大于 maxRecordFileSize 时, 则暂停记录信息。
1717
*/
1818
public static boolean isRecord = true;
1919

2020
/**
2121
* 是否上传记录的上传状态信息。true 表示上传,false 表示不上传。
22-
*
22+
* <p>
2323
* 上传条件:
2424
* 增加一条上传记录时触发,
2525
* isRecord 为 true, isUpload 为 true,
2626
* 且 记录文件大小 大于 uploadThreshold,
2727
* 且 距上次上传时间大于 minInteval .
28-
*
28+
* <p>
2929
* 上传成功后,清空记录文件文件
3030
*/
3131
public static boolean isUpload = true;
@@ -35,6 +35,7 @@ public class Config {
3535
* 默认使用当前应用的缓存目录: getCacheDir()
3636
*/
3737
public static String recordDir = null;
38+
3839
static {
3940
try {
4041
recordDir = ContextGetter.applicationContext().getCacheDir().getAbsolutePath();
@@ -45,20 +46,20 @@ public class Config {
4546

4647
/**
4748
* 记录上传信息文件最大值,单位:字节。
48-
*
49+
* <p>
4950
* 记录文件大于此值后暂停记录上传信息。
5051
*/
5152
public static int maxRecordFileSize = 2 * 1024 * 1024;
5253

5354
/**
5455
* 记录文件大于 uploadThreshold 后才可能触发上传,单位:字节。
55-
*
56+
* <p>
5657
* 以 200,CwIAAF4znMnpno0U,up.qiniu.com,183.131.7.18,80,383.0,1481014578,262144 为例,
5758
* 100 条,约 7400Byte ;50 条,约 3700; 1024 约 13.8 条
58-
*
59+
* <p>
5960
* chunkSize = 256 * 1024;putThreshold = 512 * 1024
6061
* 分片上传, 1M,最好情况下 5 个请求;10M,最好情况下 41 个请求
61-
*
62+
* <p>
6263
* 可依据客户上传频率、文件大小做调整
6364
*/
6465
public static int uploadThreshold = 4 * 1024;
@@ -71,12 +72,12 @@ public class Config {
7172

7273
/**
7374
* 上传信息收集文件的地址
74-
* */
75+
*/
7576
public static String serverURL = "https://uplog.qbox.me/log";
7677

7778
/**
7879
* 当网络切换到 wifi 下,切换到此设置
79-
* */
80+
*/
8081
public static void quick() {
8182
uploadThreshold = 1 * 1024;
8283
interval = 2;
@@ -89,7 +90,7 @@ public static void normal() {
8990

9091
/**
9192
* 网络走流量时,可切换到此设置
92-
* */
93+
*/
9394
public static void slow() {
9495
uploadThreshold = 150 * 1024;
9596
interval = 300;

library/src/main/java/com/qiniu/android/collect/UploadInfoCollector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static UploadInfoCollector getCollector() {
5656
* 若需更改 isRecord、isUpload 的值,请在此方法调用前修改。
5757
*/
5858
public static void clean() {
59-
try{
59+
try {
6060
getCollector().clean0();
6161
} catch (Exception e) {
6262
e.printStackTrace();
@@ -96,7 +96,7 @@ private void clean0() {
9696
* @throws java.io.IOException
9797
*/
9898
public static void reset() {
99-
try{
99+
try {
100100
getCollector().reset0();
101101
} catch (Exception e) {
102102
e.printStackTrace();
@@ -141,7 +141,7 @@ private void initRecordFile(File recordDir) throws IOException {
141141

142142
public static void handle(final UpToken upToken, final RecordMsg record) {
143143
try {
144-
if(Config.isRecord) {
144+
if (Config.isRecord) {
145145
getCollector().handle0(upToken, record);
146146
}
147147
} catch (Throwable t) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public void accept(String key, Object value) {
398398
try {
399399
req = requestBuilder.tag(tag).build();
400400
okhttp3.Response response = httpClient.newCall(req).execute();
401-
return buildResponseInfo(response, tag.ip, tag.duration);
401+
return buildResponseInfo(response, tag.ip, tag.duration, upToken);
402402
} catch (Exception e) {
403403
e.printStackTrace();
404404
int statusCode = NetworkError;
@@ -414,7 +414,7 @@ public void accept(String key, Object value) {
414414
}
415415

416416
HttpUrl u = req.url();
417-
return new ResponseInfo(null, statusCode, "", "", "", u.host(), u.encodedPath(), "", u.port(), 0, 0, e.getMessage());
417+
return ResponseInfo.create(null, statusCode, "", "", "", u.host(), u.encodedPath(), "", u.port(), 0, 0, e.getMessage(), upToken);
418418
}
419419
}
420420

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,43 +152,41 @@ public void complete(ResponseInfo info, JSONObject response) {
152152
/**
153153
* 上传数据,并以指定的key保存文件
154154
*
155-
* @param client HTTP连接管理器
156-
* @param data 上传的数据
157-
* @param key 上传的数据保存的文件名
158-
* @param token 上传凭证
159-
* @param options 上传时的可选参数
160-
*
155+
* @param client HTTP连接管理器
156+
* @param data 上传的数据
157+
* @param key 上传的数据保存的文件名
158+
* @param token 上传凭证
159+
* @param options 上传时的可选参数
161160
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
162161
*/
163162
public static ResponseInfo syncUpload(Client client, Configuration config, byte[] data, String key, UpToken token, UploadOptions options) {
164163
try {
165164
return syncUpload0(client, config, data, null, key, token, options);
166165
} catch (Exception e) {
167-
return new ResponseInfo(null, ResponseInfo.UnknownError, "", "", "", "", "", "", 0, 0, 0, e.getMessage()); // TODO
166+
return ResponseInfo.create(null, ResponseInfo.UnknownError, "", "", "", "", "", "", 0, 0, 0, e.getMessage(), token);
168167
}
169168
}
170169

171170
/**
172171
* 上传文件,并以指定的key保存文件
173172
*
174-
* @param client HTTP连接管理器
175-
* @param file 上传的文件
176-
* @param key 上传的数据保存的文件名
177-
* @param token 上传凭证
178-
* @param options 上传时的可选参数
179-
*
173+
* @param client HTTP连接管理器
174+
* @param file 上传的文件
175+
* @param key 上传的数据保存的文件名
176+
* @param token 上传凭证
177+
* @param options 上传时的可选参数
180178
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
181179
*/
182180
public static ResponseInfo syncUpload(Client client, Configuration config, File file, String key, UpToken token, UploadOptions options) {
183181
try {
184182
return syncUpload0(client, config, null, file, key, token, options);
185183
} catch (Exception e) {
186-
return new ResponseInfo(null, ResponseInfo.UnknownError, "", "", "", "", "", "", 0, 0, 0, e.getMessage()); // TODO
184+
return ResponseInfo.create(null, ResponseInfo.UnknownError, "", "", "", "", "", "", 0, 0, 0, e.getMessage(), token);
187185
}
188186
}
189187

190188
private static ResponseInfo syncUpload0(Client client, Configuration config, byte[] data, File file,
191-
String key, UpToken token, UploadOptions optionsIn) {
189+
String key, UpToken token, UploadOptions optionsIn) {
192190
StringMap params = new StringMap();
193191
final PostArgs args = new PostArgs();
194192
if (key != null) {
@@ -226,7 +224,6 @@ private static ResponseInfo syncUpload0(Client client, Configuration config, byt
226224
args.file = file;
227225
args.mimeType = options.mimeType;
228226
args.params = params;
229-
args.userAgentPart = token.accessKey;
230227

231228
ResponseInfo info = client.syncMultipartPost(config.zone.upHost(token.token).address.toString(), args, token);
232229

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ private static boolean areInvalidArg(final String key, byte[] data, File f, Stri
4848
} else if (token == null || token.equals("")) {
4949
message = "no token";
5050
}
51+
5152
ResponseInfo info = null;
52-
if (decodedToken == UpToken.NULL) {
53-
info = ResponseInfo.invalidToken("invalid token");
54-
}
5553
if (message != null) {
5654
info = ResponseInfo.invalidArgument(message, decodedToken);
57-
}
58-
if ((f != null && f.length() == 0) || (data != null && data.length == 0)) {
55+
} else if (decodedToken == UpToken.NULL || decodedToken == null) {
56+
info = ResponseInfo.invalidToken("invalid token");
57+
} else if ((f != null && f.length() == 0) || (data != null && data.length == 0)) {
5958
info = ResponseInfo.zeroSize(decodedToken);
6059
}
60+
6161
if (info != null) {
6262
final ResponseInfo info2 = info;
6363
AsyncRun.runInMain(new Runnable() {
@@ -189,11 +189,10 @@ public void onFailure(int reason) {
189189
/**
190190
* 同步上传文件。使用 form 表单方式上传,建议只在数据较小情况下使用此方式,如 file.size() < 1024 * 1024。
191191
*
192-
* @param data 上传的数据
193-
* @param key 上传数据保存的文件名
194-
* @param token 上传凭证
195-
* @param options 上传数据的可选参数
196-
*
192+
* @param data 上传的数据
193+
* @param key 上传数据保存的文件名
194+
* @param token 上传凭证
195+
* @param options 上传数据的可选参数
197196
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
198197
*/
199198
public ResponseInfo syncPut(byte[] data, String key, String token, UploadOptions options) {
@@ -208,11 +207,10 @@ public ResponseInfo syncPut(byte[] data, String key, String token, UploadOptions
208207
/**
209208
* 同步上传文件。使用 form 表单方式上传,建议只在文件较小情况下使用此方式,如 file.size() < 1024 * 1024。
210209
*
211-
* @param file 上传的文件对象
212-
* @param key 上传数据保存的文件名
213-
* @param token 上传凭证
214-
* @param options 上传数据的可选参数
215-
*
210+
* @param file 上传的文件对象
211+
* @param key 上传数据保存的文件名
212+
* @param token 上传凭证
213+
* @param options 上传数据的可选参数
216214
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
217215
*/
218216
public ResponseInfo syncPut(File file, String key, String token, UploadOptions options) {
@@ -227,11 +225,10 @@ public ResponseInfo syncPut(File file, String key, String token, UploadOptions o
227225
/**
228226
* 同步上传文件。使用 form 表单方式上传,建议只在文件较小情况下使用此方式,如 file.size() < 1024 * 1024。
229227
*
230-
* @param file 上传的文件绝对路径
231-
* @param key 上传数据保存的文件名
232-
* @param token 上传凭证
233-
* @param options 上传数据的可选参数
234-
*
228+
* @param file 上传的文件绝对路径
229+
* @param key 上传数据保存的文件名
230+
* @param token 上传凭证
231+
* @param options 上传数据的可选参数
235232
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
236233
*/
237234
public ResponseInfo syncPut(String file, String key, String token, UploadOptions options) {
@@ -246,17 +243,20 @@ private static ResponseInfo areInvalidArg(final String key, byte[] data, File f,
246243
} else if (token == null || token.equals("")) {
247244
message = "no token";
248245
}
249-
ResponseInfo info = null;
250-
if (decodedToken == null) {
251-
info = ResponseInfo.invalidToken("invalid token");
252-
}
246+
253247
if (message != null) {
254-
info = ResponseInfo.invalidArgument(message);
248+
return ResponseInfo.invalidArgument(message, decodedToken);
255249
}
250+
251+
if (decodedToken == UpToken.NULL || decodedToken == null) {
252+
return ResponseInfo.invalidToken("invalid token");
253+
}
254+
256255
if ((f != null && f.length() == 0) || (data != null && data.length == 0)) {
257-
info = ResponseInfo.zeroSize();
256+
return ResponseInfo.zeroSize(decodedToken);
258257
}
259-
return info;
258+
259+
return null;
260260
}
261261

262262
}

mvn_push.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ afterEvaluate { project ->
9393
}
9494

9595
task androidJavadocs(type: Javadoc) {
96+
failOnError = false
9697
source = android.sourceSets.main.java.srcDirs
9798
options.encoding = "UTF-8"
9899
options.charSet = 'UTF-8'

0 commit comments

Comments
 (0)