Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#Changelog

## 7.0.7 (2015-06-23)

### 增加
* 更详细的response info, 增加了发送字节数,id

## 7.0.6 (2015-06-04)

### 增加
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


public final class Constants {
public static final String VERSION = "7.0.6";
public static final String VERSION = "7.0.7";

public static final String UTF_8 = "utf-8";
}
17 changes: 4 additions & 13 deletions library/src/main/java/com/qiniu/android/http/HttpManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* 定义HTTP请求管理相关方法
*/
public final class HttpManager {
private static final String userAgent = getUserAgent();
private AsyncHttpClient client;
private IReport reporter;
private String backUpIp;
Expand All @@ -44,7 +43,7 @@ public HttpManager(Proxy proxy, IReport reporter, String backUpIp,
client = new AsyncHttpClient();
client.setConnectTimeout(connectTimeout*1000);
client.setResponseTimeout(responseTimeout * 1000);
client.setUserAgent(userAgent);
client.setUserAgent(UserAgent.instance().toString());
client.setEnableRedirects(true);
client.setRedirectHandler(new UpRedirectHandler());
AsyncHttpClient.blockRetryExceptionClass(CancellationHandler.CancellationException.class);
Expand Down Expand Up @@ -78,15 +77,7 @@ public HttpManager() {
this(null);
}

private static String genId() {
Random r = new Random();
return System.currentTimeMillis() + "" + r.nextInt(999);
}

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

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

ResponseHandler handler = new ResponseHandler(url, wrapper, progressHandler);
ResponseHandler handler = new ResponseHandler(url, wrapper, progressHandler, null);
if(backUpIp == null || converter != null){
client.post(null, url, h, entity, null, handler);
return;
Expand Down Expand Up @@ -165,10 +156,10 @@ public void complete(ResponseInfo info, JSONObject response) {
} catch (URISyntaxException e) {
throw new AssertionError(e);
}
ResponseHandler handler3 = new ResponseHandler(newUrl80, completionHandler, progressHandler);
ResponseHandler handler3 = new ResponseHandler(newUrl80, completionHandler, progressHandler, ip2);
client.post(null, newUrl80, h2, entity, null, handler3);
}
}), progressHandler);
}), progressHandler, ip);
client.post(null, newUrl, h2, entity, null, handler2);
}
});
Expand Down
22 changes: 14 additions & 8 deletions library/src/main/java/com/qiniu/android/http/ResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public final class ResponseHandler extends AsyncHttpResponseHandler {

private String path = null;

public ResponseHandler(String url, CompletionHandler completionHandler, ProgressHandler progressHandler) {
private volatile long sent = 0;

public ResponseHandler(String url, CompletionHandler completionHandler, ProgressHandler progressHandler, String ip) {
super(Looper.getMainLooper());
URI uri = null;
try {
Expand All @@ -69,10 +71,11 @@ public ResponseHandler(String url, CompletionHandler completionHandler, Progress
}
this.completionHandler = completionHandler;
this.progressHandler = progressHandler;
this.ip = ip;
}

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

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

return new ResponseInfo(statusCode, reqId, xlog, xvia, host, path, ip, port, duration, err);
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, path, ip, port, duration, sent, err);
}

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

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

@Override
public void onProgress(int bytesWritten, int totalSize) {
this.sent += bytesWritten;
if (progressHandler != null) {
progressHandler.onProgress(bytesWritten, totalSize);
}
Expand All @@ -197,7 +201,9 @@ protected void sendMessage(Message msg) {
if (response != null && response.length >= 4) {
Throwable e = (Throwable) response[3];
if (!(e instanceof UnknownHostException)) {
this.ip = Dns.getAddressesString(host);
if (ip == null){
this.ip = Dns.getAddressesString(host);
}
}
}
}
Expand Down
32 changes: 25 additions & 7 deletions library/src/main/java/com/qiniu/android/http/ResponseInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,22 @@ public final class ResponseInfo {
*/
public final String path;

public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, String host, String path, String ip, int port, double duration, String error) {
/**
* user agent id
*/
public final String id;

/**
* log 时间戳
*/
public final long timeStamp;

/**
* 已发送字节数
*/
public final long sent;

public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, String host, String path, String ip, int port, double duration, long sent, String error) {
this.statusCode = statusCode;
this.reqId = reqId;
this.xlog = xlog;
Expand All @@ -74,25 +89,28 @@ public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, Stri
this.error = error;
this.ip = ip;
this.port = port;
this.id = UserAgent.instance().id;
this.timeStamp = System.currentTimeMillis()/1000;
this.sent = sent;
}

public static ResponseInfo cancelled() {
return new ResponseInfo(Cancelled, "", "", "", "", "", "", -1, 0, "cancelled by user");
return new ResponseInfo(Cancelled, "", "", "", "", "", "", -1, 0, 0, "cancelled by user");
}

public static ResponseInfo invalidArgument(String message) {
return new ResponseInfo(InvalidArgument, "", "", "", "", "", "", -1, 0,
return new ResponseInfo(InvalidArgument, "", "", "", "", "", "", -1, 0, 0,
message);
}

public static ResponseInfo invalidToken(String message) {
return new ResponseInfo(InvalidToken, "", "", "", "", "", "", -1, 0,
return new ResponseInfo(InvalidToken, "", "", "", "", "", "", -1, 0,0,
message);
}

public static ResponseInfo fileError(Exception e) {
return new ResponseInfo(InvalidFile, "", "", "", "", "", "", -1,
0, e.getMessage());
0, 0, e.getMessage());
}

public boolean isCancelled() {
Expand Down Expand Up @@ -128,7 +146,7 @@ public boolean isNotQiniu() {
}

public String toString() {
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}",
super.toString(), statusCode, reqId, xlog, xvia, host, path, ip, port, duration, error);
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}",
id, statusCode, reqId, xlog, xvia, host, path, ip, port, duration, timeStamp, sent, error);
}
}
39 changes: 39 additions & 0 deletions library/src/main/java/com/qiniu/android/http/UserAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.qiniu.android.http;

import com.qiniu.android.common.Constants;

import java.util.Random;

import static java.lang.String.format;

/**
* Created by bailong on 15/6/23.
*/
public class UserAgent {
public final String id;
public final String ua;

private static UserAgent _instance = new UserAgent();
private UserAgent() {
id = genId();
ua = getUserAgent(id);
}

public static UserAgent instance(){
return _instance;
}

public String toString(){
return ua;
}

private static String genId() {
Random r = new Random();
return System.currentTimeMillis() + "" + r.nextInt(999);
}

private static String getUserAgent(String id) {
return format("QiniuAndroid/%s (%s; %s; %s)", Constants.VERSION,
android.os.Build.VERSION.RELEASE, android.os.Build.MODEL, id);
}
}