【问题标题】:Access retrofit server request and response time Android访问改造服务器请求和响应时间 Android
【发布时间】:2019-01-08 19:52:04
【问题描述】:

我可以看到,每当我进行 web svc 调用时,我都会在日志中得到响应。但是我发现很难在我的代码中实际访问该信息。例如,如何从以下日志中找到服务器请求时间和响应时间。

HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: application/json;charset=UTF-8
Date: Tue, 21 Oct 2014 12:35:26 GMT
OkHttp-Received-Millis: 1413894922514
OkHttp-Response-Source: NETWORK 200
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1413894921354
Server: Apache-Coyote/1.1
transfer-encoding: chunked

【问题讨论】:

    标签: android retrofit


    【解决方案1】:

    我正在使用的库和版本:

    Retrofit 版本:2.1.0

    okhttp 版本:3.3.1

    logging-interceptor 版本:3.3.1

    此方法仅适用于onResponse()

    如果您调试onResponse(),您将在rawResponse 部分中看到sentRequestAtMillisreceivedResponseAtMillis,如下图所示。

    这些参数分别保存发出请求和收到响应的时间(以毫秒为单位)。

    以下示例说明了如何在收到响应后以编程方式获取这些参数中的值。

    retrofitRequest.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> 
            // time when the request was made to server, which you get from ```sentRequestAtMillis```
            long requestTime = response.raw().sentRequestAtMillis();
    
            // time when the response was received, which you get from ```receivedResponseAtMillis```
            long responseTime = response.raw().receivedResponseAtMillis();
    
            //time taken to receive the response after the request was sent
            long apiTime =  responseTime - requestTime;
        }
    
        @Override public void onFailure(Call<ResponseBody> call, Throwable t) {
    
        }
    });
    

    请注意,您可能需要针对可能发生的任何异常处理代码。

    【讨论】:

    • 你有没有注意到这种做法与HttpLoggingInterceptor报道的时间不一致? AFAIS,它提供了大约一半。
    【解决方案2】:

    在 Retrofit

    警告 - this will been removed in Retrofit 2.0 将被通用拦截器系统取代。

    例如:

    new RestAdapter.Builder()
        .setEndpoint("http://www.google.com")
        .setProfiler(new Profiler() {
            @Override
            public Object beforeCall() {
              return null;
            }
    
            @Override
            public void afterCall(RequestInformation requestInfo, long elapsedTime, int statusCode, Object beforeCallData) {
              Log.d("Retrofit Profiler", String.format("HTTP %d %s %s (%dms)",
                  statusCode, requestInfo.getMethod(), requestInfo.getRelativePath(), elapsedTime));
            }
        })
        .create(MyService.class);
    

    这是一些示例输出:

    D/Retrofit Profiler﹕ HTTP 200 GET /users/me (200ms)
    

    【讨论】:

      【解决方案3】:

      在您的回调中,您可以访问具有getHeaders() 方法的Response 对象,从而为您提供一堆Headers

      如您所见,Header 只是一个名称-值对,所以我认为处理它们不会有任何问题。

      【讨论】:

      • 如何在同步改造调用中获得响应?
      • @SoH,根据文档,“要访问原始 HTTP 响应,请使用 Response 类型。@GET("/users/list") Response userList();”如果您对获取对象而不只是响应感兴趣,请查看 @ 987654323@.
      • 谢谢斯迈莱克。你的评论真的很有帮助。我还有一个问题。对于带有返回类型的改造,我如何访问响应类型,因为我已经为返回类型指定了我的自定义对象。
      • @SoH,你应该改变你的观点:将Response 作为返回类型,然后提取你的对象:link。您无法从您的对象中获取Response,但您可以从Response 中获取您的对象。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2017-02-12
      • 2016-05-16
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      相关资源
      最近更新 更多