Skip to content

Commit 41d505b

Browse files
committed
Merge pull request #126 from longbai/add_id_to_info
detail info
2 parents 5afab43 + e8d38e9 commit 41d505b

File tree

6 files changed

+88
-29
lines changed

6 files changed

+88
-29
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#Changelog
22

3+
## 7.0.7 (2015-06-23)
4+
5+
### 增加
6+
* 更详细的response info, 增加了发送字节数,id
7+
38
## 7.0.6 (2015-06-04)
49

510
### 增加

library/src/main/java/com/qiniu/android/common/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public final class Constants {
5-
public static final String VERSION = "7.0.6";
5+
public static final String VERSION = "7.0.7";
66

77
public static final String UTF_8 = "utf-8";
88
}

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
* 定义HTTP请求管理相关方法
2525
*/
2626
public final class HttpManager {
27-
private static final String userAgent = getUserAgent();
2827
private AsyncHttpClient client;
2928
private IReport reporter;
3029
private String backUpIp;
@@ -44,7 +43,7 @@ public HttpManager(Proxy proxy, IReport reporter, String backUpIp,
4443
client = new AsyncHttpClient();
4544
client.setConnectTimeout(connectTimeout*1000);
4645
client.setResponseTimeout(responseTimeout * 1000);
47-
client.setUserAgent(userAgent);
46+
client.setUserAgent(UserAgent.instance().toString());
4847
client.setEnableRedirects(true);
4948
client.setRedirectHandler(new UpRedirectHandler());
5049
AsyncHttpClient.blockRetryExceptionClass(CancellationHandler.CancellationException.class);
@@ -78,15 +77,7 @@ public HttpManager() {
7877
this(null);
7978
}
8079

81-
private static String genId() {
82-
Random r = new Random();
83-
return System.currentTimeMillis() + "" + r.nextInt(999);
84-
}
8580

86-
private static String getUserAgent() {
87-
return format("QiniuAndroid/%s (%s; %s; %s)", Constants.VERSION,
88-
android.os.Build.VERSION.RELEASE, android.os.Build.MODEL, genId());
89-
}
9081

9182
/**
9283
* 以POST方法发送请求数据
@@ -119,7 +110,7 @@ private void postEntity(String url, final HttpEntity entity, Header[] headers,
119110
url = converter.convert(url);
120111
}
121112

122-
ResponseHandler handler = new ResponseHandler(url, wrapper, progressHandler);
113+
ResponseHandler handler = new ResponseHandler(url, wrapper, progressHandler, null);
123114
if(backUpIp == null || converter != null){
124115
client.post(null, url, h, entity, null, handler);
125116
return;
@@ -165,10 +156,10 @@ public void complete(ResponseInfo info, JSONObject response) {
165156
} catch (URISyntaxException e) {
166157
throw new AssertionError(e);
167158
}
168-
ResponseHandler handler3 = new ResponseHandler(newUrl80, completionHandler, progressHandler);
159+
ResponseHandler handler3 = new ResponseHandler(newUrl80, completionHandler, progressHandler, ip2);
169160
client.post(null, newUrl80, h2, entity, null, handler3);
170161
}
171-
}), progressHandler);
162+
}), progressHandler, ip);
172163
client.post(null, newUrl, h2, entity, null, handler2);
173164
}
174165
});

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public final class ResponseHandler extends AsyncHttpResponseHandler {
5555

5656
private String path = null;
5757

58-
public ResponseHandler(String url, CompletionHandler completionHandler, ProgressHandler progressHandler) {
58+
private volatile long sent = 0;
59+
60+
public ResponseHandler(String url, CompletionHandler completionHandler, ProgressHandler progressHandler, String ip) {
5961
super(Looper.getMainLooper());
6062
URI uri = null;
6163
try {
@@ -69,10 +71,11 @@ public ResponseHandler(String url, CompletionHandler completionHandler, Progress
6971
}
7072
this.completionHandler = completionHandler;
7173
this.progressHandler = progressHandler;
74+
this.ip = ip;
7275
}
7376

7477
private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers, byte[] responseBody,
75-
String host, String path, String ip, int port, double duration, Throwable error) {
78+
String host, String path, String ip, int port, double duration, long sent, Throwable error) {
7679

7780
if (error != null && error instanceof CancellationHandler.CancellationException) {
7881
return ResponseInfo.cancelled();
@@ -141,7 +144,7 @@ private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers,
141144
}
142145
}
143146

144-
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, path, ip, port, duration, err);
147+
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, path, ip, port, duration, sent, err);
145148
}
146149

147150
private static JSONObject buildJsonResp(byte[] body) throws Exception {
@@ -159,21 +162,22 @@ public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
159162
} catch (Exception e) {
160163
exception = e;
161164
}
162-
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, path, ip, port, duration, exception);
163-
Log.i("upload----success", info.toString());
165+
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, path, ip, port, duration, sent, exception);
166+
// Log.i("upload----success", info.toString());
164167
completionHandler.complete(info, obj);
165168
}
166169

167170
@Override
168171
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
169172
double duration = (System.currentTimeMillis() - reqStartTime) / 1000.0;
170-
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, path, ip, port, duration, error);
171-
Log.i("upload----failed", info.toString());
173+
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, path, ip, port, duration, sent, error);
174+
// Log.i("upload----failed", info.toString());
172175
completionHandler.complete(info, null);
173176
}
174177

175178
@Override
176179
public void onProgress(int bytesWritten, int totalSize) {
180+
this.sent += bytesWritten;
177181
if (progressHandler != null) {
178182
progressHandler.onProgress(bytesWritten, totalSize);
179183
}
@@ -197,7 +201,9 @@ protected void sendMessage(Message msg) {
197201
if (response != null && response.length >= 4) {
198202
Throwable e = (Throwable) response[3];
199203
if (!(e instanceof UnknownHostException)) {
200-
this.ip = Dns.getAddressesString(host);
204+
if (ip == null){
205+
this.ip = Dns.getAddressesString(host);
206+
}
201207
}
202208
}
203209
}

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,22 @@ public final class ResponseInfo {
6363
*/
6464
public final String path;
6565

66-
public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, String host, String path, String ip, int port, double duration, String error) {
66+
/**
67+
* user agent id
68+
*/
69+
public final String id;
70+
71+
/**
72+
* log 时间戳
73+
*/
74+
public final long timeStamp;
75+
76+
/**
77+
* 已发送字节数
78+
*/
79+
public final long sent;
80+
81+
public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, String host, String path, String ip, int port, double duration, long sent, String error) {
6782
this.statusCode = statusCode;
6883
this.reqId = reqId;
6984
this.xlog = xlog;
@@ -74,25 +89,28 @@ public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, Stri
7489
this.error = error;
7590
this.ip = ip;
7691
this.port = port;
92+
this.id = UserAgent.instance().id;
93+
this.timeStamp = System.currentTimeMillis()/1000;
94+
this.sent = sent;
7795
}
7896

7997
public static ResponseInfo cancelled() {
80-
return new ResponseInfo(Cancelled, "", "", "", "", "", "", -1, 0, "cancelled by user");
98+
return new ResponseInfo(Cancelled, "", "", "", "", "", "", -1, 0, 0, "cancelled by user");
8199
}
82100

83101
public static ResponseInfo invalidArgument(String message) {
84-
return new ResponseInfo(InvalidArgument, "", "", "", "", "", "", -1, 0,
102+
return new ResponseInfo(InvalidArgument, "", "", "", "", "", "", -1, 0, 0,
85103
message);
86104
}
87105

88106
public static ResponseInfo invalidToken(String message) {
89-
return new ResponseInfo(InvalidToken, "", "", "", "", "", "", -1, 0,
107+
return new ResponseInfo(InvalidToken, "", "", "", "", "", "", -1, 0,0,
90108
message);
91109
}
92110

93111
public static ResponseInfo fileError(Exception e) {
94112
return new ResponseInfo(InvalidFile, "", "", "", "", "", "", -1,
95-
0, e.getMessage());
113+
0, 0, e.getMessage());
96114
}
97115

98116
public boolean isCancelled() {
@@ -128,7 +146,7 @@ public boolean isNotQiniu() {
128146
}
129147

130148
public String toString() {
131-
return String.format(Locale.ENGLISH, "{ResponseInfo:%s,status:%d, reqId:%s, xlog:%s, xvia:%s, host:%s, path:%s, ip:%s, port:%d, duration:%f s, error:%s}",
132-
super.toString(), statusCode, reqId, xlog, xvia, host, path, ip, port, duration, error);
149+
return String.format(Locale.ENGLISH, "{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}",
150+
id, statusCode, reqId, xlog, xvia, host, path, ip, port, duration, timeStamp, sent, error);
133151
}
134152
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.qiniu.android.http;
2+
3+
import com.qiniu.android.common.Constants;
4+
5+
import java.util.Random;
6+
7+
import static java.lang.String.format;
8+
9+
/**
10+
* Created by bailong on 15/6/23.
11+
*/
12+
public class UserAgent {
13+
public final String id;
14+
public final String ua;
15+
16+
private static UserAgent _instance = new UserAgent();
17+
private UserAgent() {
18+
id = genId();
19+
ua = getUserAgent(id);
20+
}
21+
22+
public static UserAgent instance(){
23+
return _instance;
24+
}
25+
26+
public String toString(){
27+
return ua;
28+
}
29+
30+
private static String genId() {
31+
Random r = new Random();
32+
return System.currentTimeMillis() + "" + r.nextInt(999);
33+
}
34+
35+
private static String getUserAgent(String id) {
36+
return format("QiniuAndroid/%s (%s; %s; %s)", Constants.VERSION,
37+
android.os.Build.VERSION.RELEASE, android.os.Build.MODEL, id);
38+
}
39+
}

0 commit comments

Comments
 (0)