【发布时间】:2020-02-14 04:50:22
【问题描述】:
我在 Debug 的 BODY 中借助 OkHttp3 拦截器获得了 Api 响应的响应时间,但我想要发布版本中每个 api 的服务器响应时间,我想将其上传以在跟踪器中进行数据分析。我已经尝试过这两种方法 1. Response.sentRequestAtMillis() 2. Response.receivedResponseAtMillis() 但是我没有成功,所以请帮助我找到每个 api 的响应时间,或者可以通过计算 sentRequestAtMillis 和 receivedResponseAtMillis 或者直接获取响应 Api 示例中显示的响应时间,例如 (31ms) 。
API 请求 :-
D/OkHttp: --> POST http://api.globoapps.in/abc/updateUserDetail
D/OkHttp: Content-Type: application/json; charset=UTF-8
D/OkHttp: Content-Length: 208
D/OkHttp: {"androidId":"996e831d34ba64b0","id":4,"deviceToken":"abcd"}
D/OkHttp: --> END POST (208-byte body)
API 响应:-
D/OkHttp: <-- 200 http://api.globoapps.in/abc/updateUserDetail (31ms)
D/OkHttp: Server: nginx/1.12.2
D/OkHttp: Date: Thu, 17 Oct 2019 09:30:24 GMT
D/OkHttp: Content-Type: application/json;charset=UTF-8
D/OkHttp: Transfer-Encoding: chunked
D/OkHttp: Connection: keep-alive
D/OkHttp: {"id":4,"status":"Success"}
D/OkHttp: <-- END HTTP (533-byte body)
代码(MainBaseApplication.java):-
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.readTimeout(30, TimeUnit.SECONDS);
httpClient.connectTimeout(30, TimeUnit.SECONDS);
httpClient.writeTimeout(30, TimeUnit.SECONDS);
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder builder = original.newBuilder();
builder.method(original.method(), original.body());
// builder.header("Accept", "application/json");
if (TOKEN.length() > 0)
builder.header("Authorization", TOKEN);
return chain.proceed(builder.build());
}
});
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
if (BuildConfig.DEBUG) {
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
} else {
interceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
}
httpClient.addInterceptor(interceptor);
OkHttpClient client = httpClient.build();
// Response response = client.newCall(request).execute();
// tx = response.sentRequestAtMillis();
// rx = response.receivedResponseAtMillis();
// System.out.println("response time : "+(rx - tx)+" ms");
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(WebAPI.BASE_URL)
.client(httpClient.build())
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
【问题讨论】:
标签: java android api retrofit2 okhttp