簡體   English   中英

How to get api request and api response time in Android Retrofit api using OkHttp Interceptor for each api?

[英]How to get api request and api response time in Android Retrofit api using OkHttp Interceptor for each api?

我得到了 Api 響應的響應時間,在調試時 BODY 中的 OkHttp3 攔截器的幫助下,但我想要每個 api 在發布版本上的服務器響應時間,我想上傳它以在跟蹤器中進行數據分析。 我已經嘗試過這兩種方法 1. Response.sentRequestAtMillis() 2. Response.receivedResponseAtMillis() 但我沒有成功,所以請幫助我找到每個 api 的響應時間,或者通過計算 sendRequestAtMillis 和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;
    }

sentRequestAtMillisreceivedResponseAtMillis參數是指設備時間而不是服務器時間,如果您更改設備時間,它也會更改為此值。

根據https://square.github.io/okhttp/4.x/okhttp/okhttp3/-response/received-response-at-millis/它是某個時間緩存的時間戳,因此此方法不適用於您。

對於您的解決方案:您必須從 api 發送時間戳,您可以從 api 的響應中獲取該參數並且您可以使用它。

如果您想要像31ms這樣的毫秒響應,您可以覆蓋 class HttpLoggingInterceptor.kt並檢查 222 行號中的變量val tookMs ,這將返回您想要的 ms .. :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM