毕加索没有用 Dagger 2 加载图像

Picasso not loading images with Dagger 2

我仍在学习 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);
    }
 }

AppComponent.java

@AppScope
@Component(modules = {AppModule.class, AndroidSupportInjectionModule.class, BuildersModule.class})
public interface AppComponent{

    void inject(App app);

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(App application);
        AppComponent build();
    }
}

App.java

这是我的 App.java class 我初始化 Dagger 的地方。

public class App extends Application implements HasActivityInjector {

    @Inject
    DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;

    @Override
    public void onCreate() {
        super.onCreate();
        Timber.plant(new Timber.DebugTree());
        DaggerAppComponent.builder()
                .application(this)
                .build()
                .inject(this);
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return dispatchingAndroidInjector;
    }
}

在我的 activity 中,我使用了字段注入 @Inject MoviesAdapter adapter; 并在我的适配器中使用了构造函数注入

@Inject
public MoviesAdapter(Picasso picasso) {
    this.picasso = picasso;
}

然后我调用了picasso.load(...)方法。但是我的 RecyclerView 的 ImageViews 上没有加载图像。下面附上的是 Picasso 日志,它没有说任何错误。可能是什么问题?我如何使用 Dagger 初始化 Picasso 有什么问题吗?非常感谢您的帮助。

D/Picasso: Main        created      [R230] Request{https://image.tmdb.org/t/p/w500//8bcpki9GfXdXj9esFpPtlate8v0.jpg}
D/Picasso: Hunter      removed      [R222]+466ms from 
D/Picasso: Dispatcher  enqueued     [R230]+4ms 
D/Picasso: Hunter      executing    [R230]+5ms 
D/Picasso: Main        created      [R231] Request{https://image.tmdb.org/t/p/w500//4nKoB6wMVXfsYgRZK5lHZ5VMQ6J.jpg}
D/Picasso: Hunter      removed      [R223]+635ms from 
D/Picasso: Dispatcher  enqueued     [R231]+5ms 
D/Picasso: Hunter      executing    [R231]+13ms 
D/Picasso: Main        created      [R232] Request{https://image.tmdb.org/t/p/w500//5LYSsOPzuP13201qSzMjNxi8FxN.jpg}
D/Picasso: Hunter      removed      [R224]+706ms from 
D/Picasso: Dispatcher  enqueued     [R232]+3ms 
D/Picasso: Hunter      executing    [R232]+4ms 
D/Picasso: Main        created      [R233] Request{https://image.tmdb.org/t/p/w500//2lIr27lBdxCpzYDl6WUHzzD6l6H.jpg}
D/Picasso: Hunter      removed      [R226]+637ms from 
D/Picasso: Dispatcher  enqueued     [R233]+2ms 
D/Picasso: Hunter      executing    [R233]+4ms 
D/Picasso: Main        created      [R234] Request{https://image.tmdb.org/t/p/w500//tCBxnZwLiY1BOKw3tH6AxHZdqPh.jpg}
D/Picasso: Hunter      removed      [R225]+736ms from 
D/Picasso: Hunter      executing    [R234]+4ms 
D/Picasso: Dispatcher  enqueued     [R234]+3ms 

编辑

onBindViewHolder() 我打电话给 picasso.load()

@Override
public void onBindViewHolder(@NonNull MoviesHolder holder, int position) {  
    picasso.load("https://image.tmdb.org/t/p/w500/" + movieList.get(position).getPosterPath()).into(holder.ivMovie);
}

我终于明白了。关于 OkHttp3Downloader 有一个问题,因为当我删除它时,毕加索可以成功加载图像。然后我尝试导入 com.squareup.picasso.OkHttp3Downloader 而不是 com.jakewharton.picasso.OkHttp3Downloader,它现在可以工作了。我真的不知道为什么我不能使用 jakewharton 的,两者之间有什么区别。