我是匕首新手2如何实现这一目标?

时间:2018-02-12 12:51:50

标签: android retrofit mvp dagger-2

这是MVP模块

@PosterFragmentScope
@Module
public class PosterFragmentModule {

    PosterFragmentMVP.View view;
    Context mContext;

    public PosterFragmentModule(PosterFragmentMVP.View view,Context mContext) {
        this.view = view;
        this.mContext = mContext;
    }

    @PosterFragmentScope
    @Provides
    public PosterFragmentPresenter providePresenter(PosterFragmentMVP.View view , PosterFragmentMVP.InterActor interActor){
        return new PosterFragmentPresenter(view,interActor,mContext);
    }

    @PosterFragmentScope
    @Provides
    public PosterFragmentMVP.View provideView(){
        return view;
    }

    @PosterFragmentScope
    @Provides
    public PosterFragmentInteractor provideInteractor(APIServices.TMDbPopular tmDbPopular,APIServices.TMDbServiceTopRated tmDbServiceTopRated){
        return new PosterFragmentInteractor(tmDbPopular,tmDbServiceTopRated);
    }

    @PosterFragmentScope
    @Provides
    public PosterFragmentMVP.Presenter providePresenterInterface(APIServices.TMDbPopular tmDbPopular,APIServices.TMDbServiceTopRated tmDbServiceTopRated){
        return providePresenter(view,provideInteractor(tmDbPopular,tmDbServiceTopRated));
    }

    @Provides
    @PosterFragmentScope
    Context provideContext() {
        return mContext;
    }

    @Provides
    @PosterFragmentScope
    public PosterAdapter provideAdapter(){
        return new PosterAdapter(mContext,new ArrayList<Movie>());
    }

    @Provides
    @PosterFragmentScope
    public APIServices.TMDbPopular provideTmDbPopular(Retrofit retrofit){
        return retrofit.create(APIServices.TMDbPopular.class);
    }

    @Provides
    @PosterFragmentScope
    public APIServices.TMDbServiceTopRated provideTmDbServiceTopRated(Retrofit retrofit){
        return retrofit.create(APIServices.TMDbServiceTopRated.class);
    }
}

此API模块

 @Module
@Singleton
public class APIModule {

    private final String BASEURL = "https://api.themoviedb.org/";

    @Provides
    @Singleton
    public OkHttpClient provideClinet (){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();

        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder().addInterceptor(interceptor).build();
    }

    @Provides
    @Singleton
    public Retrofit provideRetrofit(String base_url, OkHttpClient okHttpClient){
        return new Retrofit.Builder()
                .baseUrl(base_url)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Provides
    public APIServices.TrailersService provideTrailersService() {
        return provideRetrofit(BASEURL, provideClinet()).create(APIServices.TrailersService.class);
    }

    @Provides
    public APIServices.ReviewsService provideReviewsService() {
        return provideRetrofit(BASEURL, provideClinet()).create(APIServices.ReviewsService.class);
    }

这是APP组件

 @Component(modules = {RoomModule.class,APIModule.class})
@Singleton
public interface APPComponent {
    PosterFragmentComponent plus(PosterFragmentModule posterFragmentModule);
    MovieDetailComponent plus(MovieDetailFragmentModule movieDetailFragmentModule);
}

这是海报组件

@Subcomponent(modules =PosterFragmentModule.class)
@PosterFragmentScope
public interface PosterFragmentComponent {
    void inject(PosterFragment posterFragment);
}

当我这样做时,给我错误

  

错误:(19,8)错误:[com.example.ali.movi​​edb.DI.Components.PosterFragmentComponent.inject(com.example.ali.movi​​edb.Views.PosterFragment)]无法提供java.lang.String没有@Inject构造函数或来自@ Provide-annotated方法。 java.lang.String注入com.example.ali.movi​​edb.DI.Modules.APIModule.provideRetrofit(base_url,...)retrofit2.Retrofit注入com.example.ali.movi​​edb.DI.Modules.PosterFragmentModule.provideTmDbPopular(改造)com.example.ali.movi​​edb.Contracts.APIServices.TMDbPopular注入com.example.ali.movi​​edb.DI.Modules.PosterFragmentModule.providePresenterInterface(tmDbPopular,...)com.example.ali.movi​​edb.Contracts.PosterFragmentMVP。在com.example.movi​​edb.View.PosterFragment.presenter中注入Presenter com.example.ali.movi​​edb.Views.PosterFragment在com.example.ali.movi​​edb.DI.Components.PosterFragmentComponent.inject(posterFragment)中注入< / p>

2 个答案:

答案 0 :(得分:0)

看起来你没有直接注入Retrofit实例,所以不需要提供(具体错误似乎是找不到要为base_url参数注入的东西)。您应该可以更改为:

public Retrofit createRetrofit(String base_url, OkHttpClient okHttpClient){
    return new Retrofit.Builder()
            .baseUrl(base_url)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}
@Provides
public APIServices.TrailersService provideTrailersService() {
    return createRetrofit(BASEURL, provideClinet()).create(APIServices.TrailersService.class);
}

@Provides
public APIServices.ReviewsService provideReviewsService() {
    return createRetrofit(BASEURL, provideClinet()).create(APIServices.ReviewsService.class);
}

答案 1 :(得分:0)

  

我通过Make My API Module解决了这个问题

 @Module
@Singleton
public class APIModule {

    private final String BASEURL = "https://api.themoviedb.org/";

    @Provides
    @Singleton
    public OkHttpClient provideClinet (){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();

        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder().addInterceptor(interceptor).build();
    }

    @Provides
    @Singleton
    public Retrofit provideRetrofit(OkHttpClient okHttpClient){
        return new Retrofit.Builder()
                .baseUrl(BASEURL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
  

是否有最佳解决方案