Dagger生成多个改装拦截器实例

时间:2017-07-13 11:01:29

标签: android retrofit2 dagger-2 rx-java2

我是Dagger和Retrofit的新手。我有问题,尽管在dagger模块中声明了singleton,但仍会生成多个改装自定义拦截器实例。我只需要一个实例。

Dagger模块

@Module
public class ApiModule
{
    private Config config;

    public ApiModule(Config config) {
        this.config = config;
    }

    @Provides @Singleton
    public OkHttpClient.Builder provideOkHttpClient()
    {
        return new OkHttpClient.Builder();
    }

    @Provides @Singleton
    public AuthenticationRequestInterceptor provideRequestInterceptor()
    {
        return new AuthenticationRequestInterceptor();
    }

    @Provides @Singleton
    public Retrofit provideRetrofitInstance()
    {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        // add session headers interceptor
        httpClient.addInterceptor(provideRequestInterceptor());

        // add logging as last interceptor
        httpClient.addInterceptor(logging);  

        return new Retrofit.Builder()
                .baseUrl(getConfig().getBaseUrl())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(httpClient.build()) //for logging purpose remove later
                .build();
    }
}

改造拦截器

public class AuthenticationRequestInterceptor implements Interceptor
{
    private Map<String, String> defaultHeaders;

    public AuthenticationRequestInterceptor()
    {
        this.defaultHeaders = new HashMap<>();
    }

    public void setDefaultHeaders(Map<String, String> headers)
    {
        this.defaultHeaders = headers;
    }

    @Override
    public Response intercept(Chain chain) throws IOException
    {
        Request request = chain.request();

        Request.Builder requestBuilder = request.newBuilder();

        for (String key: this.defaultHeaders.keySet())
        {
            requestBuilder.addHeader(key, this.defaultHeaders.get(key));
        }

        return chain.proceed(requestBuilder.build());
    }
}

连接器

public class Connect
{
    @Inject Retrofit retrofit;
    @Inject Config config;
    @Inject AuthenticationRequestInterceptor headerInterceptor;

    public Connect(PercentApp application)
    {
        ((PercentApp) application).getComponent().inject(this);
  

/ **在此处设置拦截器标头,但此设置设置不同   比后者在postUser ** /

中调用的实例
     headerInterceptor.setDefaultHeaders(generateDefaultAuthHeaders());
    }

    public void postUser(Observer observer, User user)
    {
        if(retrofit != null)
        {
            ApiServices api = retrofit.create(ApiServices.class);
            Observable<HashMap> observable = api.postUser("{post something}");
            observable.
                    subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        }
    }

}

在我的应用程序中,我创建了Dagger的实例:

DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .apiModule(new ApiModule(new Config(environment)))
                .build();
  

在我的MainActivity中,我创建了Connect

的实例
Connect connect = new Connect((MyApp) getApplication());
User user = new User();
connect.postUser(new UserObserver(this), user);

现在问题是AuthenticationRequestInterceptor构造函数被调用了两次,我不明白为什么,因此我设置的头文件设置为一个不同的Interceptor实例。请帮忙。谢谢

2 个答案:

答案 0 :(得分:1)

您不应直接致电provideRequestInterceptor

@Provides @Singleton
public Retrofit provideRetrofitInstance(AuthenticationRequestInterceptor authInterceptor)
{
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    // add session headers interceptor
    httpClient.addInterceptor(authInterceptor);

    // add logging as last interceptor
    httpClient.addInterceptor(logging);  

    return new Retrofit.Builder()
            .baseUrl(getConfig().getBaseUrl())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(httpClient.build()) //for logging purpose remove later
            .build();
}

答案 1 :(得分:0)

在provideRetrofitInstance方法中创建了另一个实例OkHttpClient.Builder,您已经在ApiModule中提供了它。不错,而是用作httpClient参数。

@Module
public class ApiModule
{
   private Config config;

   public ApiModule(Config config) {
      this.config = config;
   }

   @Provides @Singleton
   public OkHttpClient.Builder provideOkHttpClient()
   {
      return new OkHttpClient.Builder();
   }

   @Provides @Singleton
   public AuthenticationRequestInterceptor provideRequestInterceptor()
   {
      return new AuthenticationRequestInterceptor();
   }

   @Provides @Singleton
   public Retrofit provideRetrofitInstance(OkHttpClient.Builder httpClient, 
        AuthenticationRequestInterceptor authInterceptor)
   {
       HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
       // set your desired log level
       logging.setLevel(HttpLoggingInterceptor.Level.BODY);


        //YOU DON'T THIS LINE, REFER TO ARGUMENT LIST
       //OkHttpClient.Builder httpClient = new OkHttpClient.Builder();


      // add session headers interceptor
      httpClient.addInterceptor(authInterceptor);

      // add logging as last interceptor
      httpClient.addInterceptor(logging);  

      return new Retrofit.Builder()
            .baseUrl(getConfig().getBaseUrl())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(httpClient.build()) //for logging purpose remove later
            .build();
    }
}