如何在模块中提供动态字符串,例如(服务器响应)?

问题描述 投票:0回答:1

我最近在我的项目中使用了带有Dagger 2的新Android Injector,发现一个问题:我想提供一个带有匕首的动态字符串,我不知道该怎么做;我从服务器收到令牌,需要将此令牌作为字段传递给OkHttp3CookieHelper的实例;我应该怎么做?

我试图将此字符串传递给Module Constructor,但我认为这不是真的!我认为也许应该使用@BindsInstance!请帮助;)

这是我的AppModule:

@Module(subcomponents = {MainActivityComponent.class})
    public class AppModule {

    private String cookie ;

    public AppModule(String Cookie){
        this.cookie = Cookie;
    }

    @Singleton
    @Provides
    Context provideContext(Application application) {
        return application;
    }

    @Singleton
    @Provides
    OkHttp3CookieHelper provideCookie(@Named("baseURL") String baseURL,
                                      @Named("csrfToken") String csrfToken) {
        OkHttp3CookieHelper cookieHelper = new OkHttp3CookieHelper();
        cookieHelper.setCookie(baseURL, csrfToken, cookie);
        return cookieHelper;
    }
    @Singleton
    @Provides
    OkHttpClient.Builder provideOkHttp(HttpLoggingInterceptor interceptor,
                                       OkHttp3CookieHelper cookieHelper) {
        final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.connectTimeout(8, TimeUnit.SECONDS);
        httpClient.readTimeout(8, TimeUnit.SECONDS);
        httpClient.callTimeout(8, TimeUnit.SECONDS);
        httpClient.cookieJar(cookieHelper.cookieJar());
        httpClient.addInterceptor(interceptor);
        return httpClient;
    }
    @Singleton
    @Provides
    HttpLoggingInterceptor provideHttpInterceptor() {
        final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return interceptor;
    }
    @Singleton
    @Provides
    GsonConverterFactory provideGsonConverterFactory() {
        return GsonConverterFactory.create();
    }
    @Singleton
    @Provides
    @Named("baseURL")
    String provideBaseURL() {
        return Const.BASE_URL;
    }
    @Singleton
    @Provides
    @Named("csrfToken")
    String provideCrfToken() {
        return Const.CRF_TOKEN;
    }
    @Singleton
    @Provides
    Api provideApi(Retrofit retrofit) {
        return retrofit.create(Api.class);
    }
    @Singleton
    @Provides
    Retrofit provideRetrofit(GsonConverterFactory converterFactory, OkHttpClient.Builder httpClient,
                             @Named("baseURL") String baseURL) {
        return new Retrofit.Builder().baseUrl(baseURL)
                                     .addConverterFactory(converterFactory)
                                     .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                                     .client(httpClient.build())
                                     .build();
    }
    @Singleton
    @Provides
    SharedPreferences provideSharedPreference(Context context) {
        return context.getSharedPreferences("myShp", Context.MODE_PRIVATE);
    }

    //and this is my AppComponent : 

       @Singleton
       @Component(modules = {AndroidInjectionModule.class, ActivityBuilder.class, AppModule.class, 
                 ViewModelFactoryModule.class})
       public interface AppComponent {
         @Component.Builder
         interface Builder {
         @BindsInstance
         Builder application(Application application);
         AppComponent build();
         }

      void inject(App app);
}

问题:dagger could not create instance from AppModule !

java android dagger-2 androidinjector
1个回答
0
投票

最后,经过大约10个小时的搜索并阅读了一些文章,我解决了我的问题:D

注意!

不需要自己传递响应令牌,也不需要将依赖项(在这种情况下为响应令牌)绑定到AppComponent中,但是如果您要执行此操作,则可以使用@ Component.Builder或@ Component.Factory,您可以使用此有用的文章:Dagger2: @Component.Factory and @SubComponent.Factory

但是要解决问题:

首先应将此依赖项添加到app.gradle文件中:

implementation 'com.github.franmontiel:PersistentCookieJar:v1.0.1'

然后在appMoule类中提供PersistentCookieJar的实例,然后传递给我们的OkHttpClient实例:

@Singleton
@Provides
PersistentCookieJar ProvideCookieJar(Context context) {
    return new PersistentCookieJar(new SetCookieCache(),
                                   new SharedPrefsCookiePersistor(context));
}

@Singleton
@Provides
OkHttpClient.Builder provideOkHttp(HttpLoggingInterceptor interceptor,
                                   PersistentCookieJar persistentCookieJar) {
    final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.connectTimeout(8, TimeUnit.SECONDS);
    httpClient.readTimeout(8, TimeUnit.SECONDS);
    httpClient.callTimeout(8, TimeUnit.SECONDS);
    httpClient.cookieJar(persistentCookieJar);
    httpClient.addInterceptor(interceptor);
    return httpClient;
}

不要忘记提供需要创建PersistentCookieJar实例的Context:

@Provides
@Singleton
Context provideContext(Application application) {
    return application;
}

并且在您的Appication类中应绑定您的应用程序类:

DaggerAppComponent.builder()
                      .application(this)
                      .build()
                      .inject(this);

和您的AppComponent应该像这样:

@Singleton
@Component(modules = {AndroidInjectionModule.class, ActivityBuilder.class, 
                      AppModule.class, ViewModelFactoryModule.class})
public interface AppComponent {
   @Component.Builder
   interface Builder {
      @BindsInstance
      Builder application(Application application);
      AppComponent build();
   }

  void inject(App app);
}

确定一切都做好!现在您的请求包含cookie并成功获取服务器响应:)

© www.soinside.com 2019 - 2024. All rights reserved.