如何使用 Retrofit 2 获得 API 请求/响应时间
How to get API Request/ Response time using Retrofit 2
我在 android 应用程序中使用 Retrofit 2.1.0 进行 API 解析。我需要改造花费的时间来解析 API。
如何使用Retrofit 2获取请求/响应时间
下面是我用于 API 解析的 Retrofit Rest 客户端调用。
public static class ServiceGenerated {
static OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder ongoing = chain.request().newBuilder();
return chain.proceed(ongoing.build());
}
})
.build();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(RetrofitUtils.API_BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
试试这个
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(httpLoggingInterceptor);
}
还添加
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
到您的应用程序模块 gradle 文件
响应示例:
Date: Fri, 12 Aug 2016 07:33:23 GMT
OkHttp-Sent-Millis: 1470987074283
OkHttp-Received-Millis: 1470987074422
您可以通过减去收到响应时的时间戳和发送请求时的时间戳来计算总的往返时间。 Response 对象的这两个方法将为您提供这两个值
Response.sentRequestAtMillis()
Response.receivedResponseAtMillis()
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
long tx = response.sentRequestAtMillis();
long rx = response.receivedResponseAtMillis();
System.out.println("response time : "+(rx - tx)+" ms");
很容易在响应的 raw()
部分找到 receivedResponseAtMillis()
和 sendResponseAtMillis()
,然后计算差值
response.raw().receivedResponseAtMillis()
response.raw().sendResponseAtMillis()
我在 android 应用程序中使用 Retrofit 2.1.0 进行 API 解析。我需要改造花费的时间来解析 API。
如何使用Retrofit 2获取请求/响应时间
下面是我用于 API 解析的 Retrofit Rest 客户端调用。
public static class ServiceGenerated {
static OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder ongoing = chain.request().newBuilder();
return chain.proceed(ongoing.build());
}
})
.build();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(RetrofitUtils.API_BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
试试这个
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(httpLoggingInterceptor);
}
还添加
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
到您的应用程序模块 gradle 文件
响应示例:
Date: Fri, 12 Aug 2016 07:33:23 GMT
OkHttp-Sent-Millis: 1470987074283
OkHttp-Received-Millis: 1470987074422
您可以通过减去收到响应时的时间戳和发送请求时的时间戳来计算总的往返时间。 Response 对象的这两个方法将为您提供这两个值
Response.sentRequestAtMillis()
Response.receivedResponseAtMillis()
RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); long tx = response.sentRequestAtMillis(); long rx = response.receivedResponseAtMillis(); System.out.println("response time : "+(rx - tx)+" ms");
很容易在响应的 raw()
部分找到 receivedResponseAtMillis()
和 sendResponseAtMillis()
,然后计算差值
response.raw().receivedResponseAtMillis()
response.raw().sendResponseAtMillis()