使HttpLoggingInterceptor不记录图像 [英] Make HttpLoggingInterceptor do not log images

查看:64
本文介绍了使HttpLoggingInterceptor不记录图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试清除日志中的垃圾,如

*�$ʞx���J/

当我收到图像时

所以我尝试重写HttpLoggingInterceptor intercept(),以检测响应中是否有Content-Type=>图像/jpeg头,但HttpLoggingInterceptor是最终的,所以我无法扩展它:(

RetrofitModule中的代码:

OkHttpClient provideOkHttpClient(Context context, Application app, Preferences preferences) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.HEADERS);

        Cache cache = new Cache(app.getCacheDir(), cacheSize);

        return new OkHttpClient.Builder()
                .addNetworkInterceptor(new OkHttpInterceptor(context, preferences))
                .addInterceptor(loggingInterceptor)
                .cache(cache)
                .build();
    }

如何在我的项目中禁用映像记录?

推荐答案

所以,因为没有人知道答案,所以我发明了自己的自行车:

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            if(!message.contains("�")){
                Timber.d(message);
            }
        }
    });

我不太确定String.containes()是否足够便宜,可以像这样使用它,但已经达到了目标

这篇关于使HttpLoggingInterceptor不记录图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆