Skip to content

Commit bcf90b2

Browse files
authored
Merge pull request #211 from zshbleaker/master
Use OkHttp's interceptor to measure request time.
2 parents 18e2bd8 + 4806ada commit bcf90b2

File tree

1 file changed

+11
-10
lines changed
  • library/src/main/java/com/qiniu/android/http

1 file changed

+11
-10
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,19 @@ public List<InetAddress> lookup(String hostname) throws UnknownHostException {
7979
@Override
8080
public okhttp3.Response intercept(Chain chain) throws IOException {
8181
Request request = chain.request();
82-
82+
final long before = System.currentTimeMillis();
8383
okhttp3.Response response = chain.proceed(request);
84-
IpTag tag = (IpTag) request.tag();
84+
final long after = System.currentTimeMillis();
85+
86+
ResponseTag tag = (ResponseTag) request.tag();
8587
String ip = "";
8688
try {
8789
ip = chain.connection().socket().getRemoteSocketAddress().toString();
8890
} catch (Exception e) {
8991
e.printStackTrace();
9092
}
9193
tag.ip = ip;
94+
tag.duration = after - before;
9295
return response;
9396
}
9497
});
@@ -156,13 +159,11 @@ public void run() {
156159
};
157160

158161
requestBuilder.header("User-Agent", UserAgent.instance().toString());
159-
final long start = System.currentTimeMillis();
160-
IpTag tag = new IpTag();
162+
ResponseTag tag = new ResponseTag();
161163
httpClient.newCall(requestBuilder.tag(tag).build()).enqueue(new Callback() {
162164
@Override
163165
public void onFailure(Call call, IOException e) {
164166
e.printStackTrace();
165-
long duration = (System.currentTimeMillis() - start) / 1000;
166167
int statusCode = ResponseInfo.NetworkError;
167168
String msg = e.getMessage();
168169
if (e instanceof CancellationHandler.CancellationException) {
@@ -178,16 +179,15 @@ public void onFailure(Call call, IOException e) {
178179
}
179180

180181
HttpUrl u = call.request().url();
181-
ResponseInfo info = new ResponseInfo(null, statusCode, "", "", "", u.host(), u.encodedPath(), "", u.port(), duration, 0, e.getMessage());
182+
ResponseInfo info = new ResponseInfo(null, statusCode, "", "", "", u.host(), u.encodedPath(), "", u.port(), 0, 0, e.getMessage());
182183

183184
complete.complete(info, null);
184185
}
185186

186187
@Override
187188
public void onResponse(Call call, okhttp3.Response response) throws IOException {
188-
long duration = (System.currentTimeMillis() - start) / 1000;
189-
IpTag tag = (IpTag) response.request().tag();
190-
onRet(response, tag.ip, duration, complete);
189+
ResponseTag tag = (ResponseTag) response.request().tag();
190+
onRet(response, tag.ip, tag.duration, complete);
191191
}
192192
});
193193
}
@@ -304,7 +304,8 @@ public void run() {
304304

305305
}
306306

307-
private static class IpTag {
307+
private static class ResponseTag {
308308
public String ip = null;
309+
public long duration = null;
309310
}
310311
}

0 commit comments

Comments
 (0)