如何使用 OkHttp 跟踪详细的请求时间。
我想得到:
我尝试使用拦截器机制,但它仅提供总请求时间。
class LoggingInterceptor implements Interceptor {
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
logger.info(String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
logger.info(String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
}
// sample request
String post(String url, String json) throws IOException {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client …Run Code Online (Sandbox Code Playgroud)