Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OkHttp发送的毫秒和OkHttp接收的毫秒不会打印在日志上:我想收集度量_Android_Retrofit2_Okhttp3 - Fatal编程技术网

Android OkHttp发送的毫秒和OkHttp接收的毫秒不会打印在日志上:我想收集度量

Android OkHttp发送的毫秒和OkHttp接收的毫秒不会打印在日志上:我想收集度量,android,retrofit2,okhttp3,Android,Retrofit2,Okhttp3,我想计算RESTAPI Web服务调用需要多少时间。我知道OkHttp/REFRONATION打印日志语句,日志中有OkHttp接收的毫秒数和OkHttp发送的毫秒数。 但我没有看到任何这些声明被打印出来。 我所看到的是: 我在Android Studio LogCat中看到的内容 2019-08-04 10:04:36.765 6651-7080/com.my.project D/OkHttp: Content-Type: application/json 2019-08-04 10:04:3

我想计算RESTAPI Web服务调用需要多少时间。我知道OkHttp/REFRONATION打印日志语句,日志中有
OkHttp接收的毫秒数
OkHttp发送的毫秒数
。 但我没有看到任何这些声明被打印出来。 我所看到的是:

我在Android Studio LogCat中看到的内容

2019-08-04 10:04:36.765 6651-7080/com.my.project D/OkHttp: Content-Type: application/json
2019-08-04 10:04:36.765 6651-7080/com.my.project D/OkHttp: Content-Length: 189
2019-08-04 10:04:36.765 6651-7080/com.my.project D/OkHttp: x-api-key: 6a6bacf-5b2-11e-911-005056b6
2019-08-04 10:04:36.770 6651-7080/com.my.project D/OkHttp: {"email_id":"checkthis@cool.com","first_name":"Amber","gender":"FEMALE","last_name":"Heard","password":"Password1!","phone_number":"123456","reset_enabled":false,"username":"iamchandra7"}
2019-08-04 10:04:36.770 6651-7080/com.my.project D/OkHttp: --> END POST (189-byte body)
2019-08-04 10:04:38.386 6651-7080/com.my.project D/OkHttp: <-- 400 Bad Request https://mywebserice.herokuapp.com/middleservice/api/v1/users/ (1611ms)
2019-08-04 10:04:38.387 6651-7080/com.my.project D/OkHttp: Date: Sun, 04 Aug 2019 17:04:38 GMT
2019-08-04 10:04:38.388 6651-7080/com.my.project D/OkHttp: Server: Apache
2019-08-04 10:04:38.389 6651-7080/com.my.project D/OkHttp: X-Frame-Options: SAMEORIGIN
2019-08-04 10:04:38.390 6651-7080/com.my.project D/OkHttp: X-XSS-Protection: 1; mode=block
2019-08-04 10:04:38.390 6651-7080/com.my.project D/OkHttp: X-Content-Type-Options: nosniff
2019-08-04 10:04:38.392 6651-7080/com.my.project D/OkHttp: Content-Type: application/json;charset=ISO-8859-1
2019-08-04 10:04:38.392 6651-7080/com.my.project D/OkHttp: Content-Length: 258
2019-08-04 10:04:38.395 6651-7080/com.my.project D/OkHttp: Strict-Transport-Security: max-age=31536000; includeSubdomains
2019-08-04 10:04:38.396 6651-7080/com.my.project D/OkHttp: Connection: close
2019-08-04 10:04:38.418 6651-7080/com.my.project D/OkHttp: {"status":"BAD_REQUEST","message":"Invalid Request","errors":["Password does not meet the requirements. It should not contain username, first name or last name. It should not contain any dictionary words. It should not be one of the last 12 passwords used"]}
2019-08-04 10:04:38.419 6651-7080/com.my.project D/OkHttp: <-- END HTTP (258-byte body)
我的代码:

    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
    //implementation 'com.squareup.okhttp:okhttp:2.6.0'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
渐变文件:

    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
    //implementation 'com.squareup.okhttp:okhttp:2.6.0'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
我的改装实例

   @Singleton
    @Provides
    @Named("myProjectRetrofit")
    Retrofit providesMyProjectRetrofit(@Named("myProjeect-server-url") String baseUrl){
        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(getHttpClient().build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    private OkHttpClient.Builder getHttpClient() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
        okHttpClient.addInterceptor(interceptor);
        okHttpClient.connectTimeout(Constants.connectTimeout, TimeUnit.MINUTES);
        okHttpClient.readTimeout(Constants.readTimeout, TimeUnit.SECONDS);
        okHttpClient.writeTimeout(Constants.writeTimeout, TimeUnit.SECONDS);

        return okHttpClient;
    }

您必须创建一个自定义拦截器。拦截器将允许您记录有关网络请求的更多信息。祝你好运


“我了解到OkHttp/REFRONT打印日志语句时,日志中包含OkHttp接收的毫秒数和OkHttp发送的毫秒数”——不在当前版本的
HttpLoggingInterceptor
中。例如,见。请求所用的时间量显示为响应的一部分(请参阅输出中的
(1611ms)
值)。@commonware:您能告诉我是否有更好的方法读取该值吗?如果您想在代码中获取该值,请创建一个自定义拦截器,并根据时间戳自行计算。我也遵循了同样的方法。但它并没有打印我正在寻找的值
OkHttp收到的毫秒数
OkHttp发送的毫秒数
。正如@commonware提到的,它在当前版本中不再打印。你还有其他想法吗?@Chandra:自定义拦截器——意思是你自己编写的
拦截器
实现——只会将你编程要打印的内容打印到Logcat。因此,如果它没有记录这些行,那是因为您没有编写代码来记录这些行。您的问题表明您使用了一个现有的拦截器(
HttpLoggingInterceptor
),而没有编写自定义的拦截器。请澄清我的答案以了解更多信息。
class MyInterceptor: Interceptor {

override fun intercept(chain: Interceptor.Chain: Response {
          // Do your work here!!
     }
}