我仍在學習 Dagger 2 并嘗試使用它創建一個簡單的應用程序。我無法讓 Picasso 工作,因為我在日志中看不到任何錯誤。這是我的代碼AppModule.java@Module(includes = {AndroidInjectionModule.class, NetworkModule.class, ViewModelModule.class})public class AppModule { ... @Provides @AppScope Picasso picasso(App app, OkHttp3Downloader okHttp3Downloader) { return new Picasso.Builder(app.getApplicationContext()) .downloader(okHttp3Downloader) .loggingEnabled(true) .build(); } ...}NetworkModule.java這是 OkHttp3Downloader 依賴項所在的位置。@Modulepublic class NetworkModule { @Provides @AppScope HttpLoggingInterceptor loggingInterceptor() { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> Timber.i(message)); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); return interceptor; } @Provides @AppScope public File file(App app) { return new File(app.getApplicationContext().getCacheDir(), "okhttp_cache"); } @Provides @AppScope Cache cache(File file) { return new Cache(file, 10 * 1000 * 1000); } @Provides @AppScope OkHttpClient okHttpClient(HttpLoggingInterceptor loggingInterceptor, Cache cache) { return new OkHttpClient.Builder() .addInterceptor(loggingInterceptor) .cache(cache) .build(); } @Provides @AppScope OkHttp3Downloader okHttp3Downloader(OkHttpClient okHttpClient) { return new OkHttp3Downloader(okHttpClient); } }
1 回答
largeQ
TA貢獻2039條經驗 獲得超8個贊
我終于弄明白了。OkHttp3Downloader 存在問題,因為當我刪除它時,畢加索可以成功加載圖像。然后我嘗試導入 com.squareup.picasso.OkHttp3Downloader 而不是 com.jakewharton.picasso.OkHttp3Downloader,現在它可以工作了。我真的不知道為什么我不能使用 jakewharton 的,但兩者之間有什么區別。
添加回答
舉報
0/150
提交
取消
