使 HttpLoggingInterceptor 不记录图像

whe*_*max 13 logging android retrofit okhttp

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

*?$?x???J/

当我收到图像时

因此,我尝试覆盖 HttpLoggingInterceptor 拦截(),以检测响应中是否存在 Content-Type => image/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();
    }
Run Code Online (Sandbox Code Playgroud)

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

whe*_*max 11

所以,既然没有人有答案,我就发明了我自己的自行车:

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            if(!message.contains("?")){
                Timber.d(message);
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

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