Android Retrofit日志不显示

哈里什·贾亚尼(Harish Gyanani)

改装不打印日志。日志级别设置为详细。也尝试过HttpLoggingInterceptor。

使用这些等级

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

代码

OkHttpClient httpClient = new OkHttpClient();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.newBuilder().connectTimeout(6, TimeUnit.MINUTES)
                .readTimeout(6, TimeUnit.MINUTES)
                .writeTimeout(6, TimeUnit.MINUTES)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request original = chain.request();
                        Request.Builder requestBuilder = original.newBuilder()
                                .header("Content-Type", "application/json")
                                .header("Authorization", "aDRF@F#JG_a34-n3d")
                                .method(original.method(), original.body());
                        Request request = requestBuilder.build();
                        return chain.proceed(request);
                    }
                })
    .addInterceptor(httpLoggingInterceptor);


return httpClient;
Mariusz-

实际上,我不知道为什么您看不到日志,但这是我的代码的一部分,可以正常工作:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
httpClientBuilder.addInterceptor(logging);

String creditentials = username + ":" + password;
_base64 = "Basic " + Base64.encodeToString(creditentials.getBytes(), Base64.NO_WRAP);

Interceptor authorization = chain -> {
    Request newRequest = chain.request().newBuilder()
            .addHeader("Authorization", _base64)
            .addHeader("Accept", "application/json")
            .build();
    return chain.proceed(newRequest);
};

httpClientBuilder.addInterceptor(authorization);

_retrofit = new Retrofit.Builder()
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(API_URL)
        .client(httpClientBuilder.build())
        .build();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章