我在我的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);
}
}发布于 2016-08-12 17:11:38
尝尝这个
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发布于 2017-04-21 19:32:33
您可以通过接收响应的时间戳和发送请求的时间戳减去总的往返时间。Response对象中的这两个方法将为您提供这两个值
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");
发布于 2019-01-15 15:46:08
这很容易,您可以在响应的raw()部分中找到receivedResponseAtMillis()和sendResponseAtMillis(),然后您就可以计算差值
response.raw().receivedResponseAtMillis()
response.raw().sendResponseAtMillis()https://stackoverflow.com/questions/38873321
复制相似问题