相关疑难解决方法(0)

使用 OkHttp 分析 http 请求

如何使用 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)

android okhttp

6
推荐指数
1
解决办法
4652
查看次数

标签 统计

android ×1

okhttp ×1