有没有办法在OkHttp客户端获取当前的请求数?

时间:2015-03-17 10:20:38

标签: android okhttp

有没有办法在OkHTTP库上获取当前运行/等待请求的数量?

我发现了类似的内容,但它返回了请求的总数。

Log.d(TAG, String.format("Hit count=%d.", getOkHttpClient().getCache().getHitCount()));
Log.d(TAG, String.format("Network count=%d.", getOkHttpClient().getCache().getNetworkCount()));
Log.d(TAG, String.format("EoRequestSpec count=%d.", getOkHttpClient().getCache().getRequestCount()));

我有意使用拦截器,但想知道更好的方法。

修改

我添加了测试应用的日志输出。您可以看到有1个活动连接,但getConnectionCount()getHttpConnectionCount()都返回0。

03-23 15:28:12.559: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:29] Sending request http://xyz.xyz.xyz.xyz/my_first_script.php on null
03-23 15:28:13.037: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:13.038: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.
03-23 15:28:13.537: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:13.540: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.
03-23 15:28:13.728: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:44] Received response for http://xyz.xyz.xyz.xyz/my_first_script.php in 1176,8ms
03-23 15:28:13.728: I/LoggingInterceptor(23131): X-Powered-By: PHP
03-23 15:28:13.728: I/LoggingInterceptor(23131): Content-type: text/html
03-23 15:28:13.728: I/LoggingInterceptor(23131): Transfer-Encoding: chunked
03-23 15:28:13.728: I/LoggingInterceptor(23131): Date: Mon, 23 Mar 2015 14:28:13 GMT
03-23 15:28:13.728: I/LoggingInterceptor(23131): Server: my_server
03-23 15:28:13.728: I/LoggingInterceptor(23131): OkHttp-Selected-Protocol: http/1.1
03-23 15:28:13.728: I/LoggingInterceptor(23131): OkHttp-Sent-Millis: 1427120892821
03-23 15:28:13.728: I/LoggingInterceptor(23131): OkHttp-Received-Millis: 1427120893720
03-23 15:28:13.735: D/MainActivity(23131): [com.myapp.android.MainActivity$6.onResponse:281]

03-23 15:28:13.737: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:29] Sending request http://xyz.xyz.xyz.xyz/my_second_script.php
03-23 15:28:14.056: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:14.058: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.
03-23 15:28:14.536: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:14.538: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.
03-23 15:28:15.036: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:15.038: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.
03-23 15:28:15.536: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:15.538: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.
03-23 15:28:15.873: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:44] Received response for http://xyz.xyz.xyz.xyz/my_second_script.php
03-23 15:28:15.873: I/LoggingInterceptor(23131): X-Powered-By: PHP
03-23 15:28:15.873: I/LoggingInterceptor(23131): Content-type: application/json; charset=UTF-8;
03-23 15:28:15.873: I/LoggingInterceptor(23131): Content-Length: 276
03-23 15:28:15.873: I/LoggingInterceptor(23131): Date: Mon, 23 Mar 2015 14:28:15 GMT
03-23 15:28:15.873: I/LoggingInterceptor(23131): Server: my_server
03-23 15:28:15.873: I/LoggingInterceptor(23131): OkHttp-Selected-Protocol: http/1.1
03-23 15:28:15.873: I/LoggingInterceptor(23131): OkHttp-Sent-Millis: 1427120893743
03-23 15:28:15.873: I/LoggingInterceptor(23131): OkHttp-Received-Millis: 1427120895870
03-23 15:28:15.876: D/MainActivity(23131): [com.myapp.android.MainActivity$5.onResponse:251]

03-23 15:28:15.905: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:29] Sending request http://xyz.xyz.xyz.xyz/my_second_script.php
03-23 15:28:16.036: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:16.039: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.

...

03-23 15:28:27.536: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:27.538: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.
03-23 15:28:27.706: D/MainActivity(23131): [com.myapp.android.MainActivity$9.run:364] Canceling a call.
03-23 15:28:27.711: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:44] Received response for  in 11804,4ms
03-23 15:28:27.714: W/MainActivity(23131): [com.myapp.android.MainActivity$8.onFailure:337] (sendWaitForNewDeviceRequestOverOkHttp)
03-23 15:28:27.714: W/MainActivity(23131): java.io.IOException: Canceled
03-23 15:28:27.714: W/MainActivity(23131):  at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:159)
03-23 15:28:27.714: W/MainActivity(23131):  at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
03-23 15:28:27.714: W/MainActivity(23131):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-23 15:28:27.714: W/MainActivity(23131):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-23 15:28:27.714: W/MainActivity(23131):  at java.lang.Thread.run(Thread.java:856)

03-23 15:28:27.717: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:29] Sending request http://xyz.xyz.xyz.xyz/my_second_script.php
03-23 15:28:28.036: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:28.037: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.

...

03-23 15:28:31.036: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:31.037: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.
03-23 15:28:31.231: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:44] Received response for http://xyz.xyz.xyz.xyz/my_second_script.php
03-23 15:28:31.231: I/LoggingInterceptor(23131): X-Powered-By: PHP
03-23 15:28:31.231: I/LoggingInterceptor(23131): Content-type: application/json; charset=UTF-8;
03-23 15:28:31.231: I/LoggingInterceptor(23131): Content-Length: 276
03-23 15:28:31.231: I/LoggingInterceptor(23131): Date: Mon, 23 Mar 2015 14:28:30 GMT
03-23 15:28:31.231: I/LoggingInterceptor(23131): Server: my_server
03-23 15:28:31.231: I/LoggingInterceptor(23131): OkHttp-Selected-Protocol: http/1.1
03-23 15:28:31.231: I/LoggingInterceptor(23131): OkHttp-Sent-Millis: 1427120907727
03-23 15:28:31.231: I/LoggingInterceptor(23131): OkHttp-Received-Millis: 1427120911228
03-23 15:28:31.234: D/MainActivity(23131): [com.myapp.android.MainActivity$5.onResponse:251]

03-23 15:28:31.243: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:29] Sending request http://xyz.xyz.xyz.xyz/my_second_script.php
03-23 15:28:31.536: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:31.537: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.

...

03-23 15:28:35.536: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=0.
03-23 15:28:35.538: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=0.
03-23 15:28:35.842: I/LoggingInterceptor(23131): [com.myapp.android.networking.interceptor.LoggingInterceptor.intercept:44] Received response for http://xyz.xyz.xyz.xyz/my_second_script.php
03-23 15:28:35.842: I/LoggingInterceptor(23131): X-Powered-By: PHP
03-23 15:28:35.842: I/LoggingInterceptor(23131): Content-type: application/json; charset=UTF-8;
03-23 15:28:35.842: I/LoggingInterceptor(23131): Content-Length: 1149
03-23 15:28:35.842: I/LoggingInterceptor(23131): Date: Mon, 23 Mar 2015 14:28:35 GMT
03-23 15:28:35.842: I/LoggingInterceptor(23131): Server: my_server
03-23 15:28:35.842: I/LoggingInterceptor(23131): OkHttp-Selected-Protocol: http/1.1
03-23 15:28:35.842: I/LoggingInterceptor(23131): OkHttp-Sent-Millis: 1427120911246
03-23 15:28:35.842: I/LoggingInterceptor(23131): OkHttp-Received-Millis: 1427120915840
03-23 15:28:35.845: D/MainActivity(23131): [com.myapp.android.MainActivity$3.onResponse:170]
03-23 15:28:36.036: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=1.
03-23 15:28:36.038: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=1.
03-23 15:28:36.536: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:78] Connection count=1.
03-23 15:28:36.537: D/MainActivity(23131): [com.myapp.android.MainActivity$1$1.run:82] HTTP connection count=1.
...

ConnectionPool类的Java文档说:

  

管理HTTP和SPDY连接的重用以减少网络   潜伏。共享相同{@link的HTTP请求   com.squareup.okhttp.Address}可以共享{@link   com.squareup.okhttp.Connection}。这个类实现了策略   哪些连接保持开放以供将来使用。

2 个答案:

答案 0 :(得分:3)

从版本3开始,可以使用queuedCallsCountrunningCallsCount

答案 1 :(得分:1)

您需要检查客户端实例中的ConnectionPool

int connections = getOkHttpClient().getConnectionPool().getConnectionCount();
int httpConnections = getOkHttpClient().getConnectionPool().getHttpConnectionCount();