我仍在学习 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 依赖项所在的位置。
@Module
public 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);
}
}
largeQ
相关分类