android - 使 HttpLoggingInterceptor 不记录图像

标签 android kotlin logging retrofit okhttp

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

*�$ʞx���J/

当我收到一张图片时

所以我尝试重写 HttpLoggingInterceptor intercept(),以检测响应中是否有 Content-Type => image/jpeg header ,但是 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.contains() 是否足够便宜以像这样使用它,但目标已达到

关于android - 使 HttpLoggingInterceptor 不记录图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46340230/

相关文章:

java - 所有安卓设备都有文件夹 "sdcard"吗?

android - ImageButton OnClickListener 不工作

java - slf4j添加自定义方法

php - 000webhost 服务网站数据库错误

android - 当 PagerAdapter.getPageWidth() < 1f 时,使所有 ViewPager 项目居中对齐

android - 播放新歌曲时收到IllegalStateException

android - 如何在自动化构建中启动 Android JUnit 测试?

kotlin - 如何在 Kotlin 中的委托(delegate)属性上添加 JPA 注释?

android - Kotlin 对委托(delegate)属性的注解

python - 如何在 raven/sentry 中记录带有上下文的完整调用堆栈?