【问题标题】:How to get api request and api response time in Android Retrofit api using OkHttp Interceptor for each api?如何使用每个 api 的 OkHttp 拦截器在 Android Retrofit api 中获取 api 请求和 api 响应时间?
【发布时间】: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


    【解决方案1】:

    sentRequestAtMillisreceivedResponseAtMillis 参数是指设备时间而不是服务器时间,如果您更改设备时间,它也会更改为该值。

    根据https://square.github.io/okhttp/4.x/okhttp/okhttp3/-response/received-response-at-millis/,它是某个时间缓存的时间戳,因此此方法不适合您。

    对于您的解决方案:您必须从 api 发送时间戳,并且您可以从 api 的响应中获取该参数并且您可以使用它。

    如果您想要像 31ms 这样的毫秒级响应,您可以覆盖 HttpLoggingInterceptor.kt 类并检查 222 行号中的变量 val tookMs,这将返回您想要的毫秒数.. :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-07
      • 2017-09-19
      • 2017-08-22
      • 2021-05-05
      • 2021-02-15
      • 2019-02-13
      • 2016-04-16
      • 2020-06-21
      相关资源
      最近更新 更多