0

Dagger2 realization in App

Network component's module

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient() {

        return new OkHttpClient.Builder().addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request.Builder builder = chain.request().newBuilder()
                        .addHeader("Accept", "application/json")
                        .addHeader("Client-Id", Constants.CONFIG.APP_NAME)
                        .addHeader("RE-Phone-Number", phoneNumber)
                        .addHeader("RE-Access-Token", access_token);
                Request request = builder.build();
                return chain.proceed(request);
            }
        }).build();
    }

It is called at the beginnig of app once. I need to change user credentials after their obtaining.

@Provides
@Singleton
Retrofit provideRetrofit(GsonConverterFactory gsonConverterFactory,
                         RxJavaCallAdapterFactory rxJavaCallAdapterFactory,
                         OkHttpClient okHttpClient) {
    return new Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(okHttpClient)
            .addConverterFactory(gsonConverterFactory)
            .addCallAdapterFactory(rxJavaCallAdapterFactory)
            .build();
}

Api component's module

@Module
public class ApiModule {

@Provides
@PerFragment
CompanyService provideCompanyService(Retrofit retrofit){
    return retrofit.create(CompanyService.class);
}

}

  • 1
    Where and how are `phoneNumber` and `access_token` declared? If you change their values, won't the next request use the updated value? – Michael Oct 06 '16 at 14:31
  • @Michael, OkHttpClient is initialized in custom Application extention class. And it does it only once. I inject OkHttpClient into Retrofit implementation. Also only once. I added code – Чечил Крым Oct 08 '16 at 18:14

0 Answers0