0

I am using reflection for change retrofit base url dynamically but I can change once in activity, after that I can not change it. Why it is not working when I try to change multiple times.

Set dynamic base url using Retrofit 2.0 and Dagger 2

I do not know the domain I just know the end points, I get the domain adress from user.

Emre Akcan
  • 982
  • 10
  • 27

1 Answers1

1

Just use different modules and api for different URLs. And then use @Named annotation to determine the correct api. Api module with HOST url

@Module
class ApiModule {
    @Provides
    @Singleton
    @Named("Interceptor")
    fun provideInterceptor(context: Context, sharedPrefsStorage: SharedPrefsStorage, @Named("ApiAuth") apiAuth: ApiAuth,realmProvider: DbProvider<Realm>): Interceptor {
        return ApiInterceptor(context, sharedPrefsStorage, apiAuth,realmProvider)
    }

    @Provides
    @Singleton
    @Named("HttpClient")
    fun provideOkHttpClient(@Named("Interceptor") interceptor: Interceptor): OkHttpClient {
        val builder = OkHttpClient.Builder()
        builder.addInterceptor(interceptor)
        builder.readTimeout(30, TimeUnit.SECONDS)
        builder.connectTimeout(30, TimeUnit.SECONDS)

            val loggingInterceptor = HttpLoggingInterceptor()
            loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            builder.addInterceptor(loggingInterceptor)
        return builder.build()
    }

    @Provides
    @Singleton
    @Named("Retrofit")
    fun provideRetrofitBuilder(@Named("HttpClient") okHttpClient: OkHttpClient): Retrofit.Builder {
        return Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(
                        GsonBuilder()
                                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                                .create()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .baseUrl(BuildConfig.HOST)
    }


    @Provides
    @Singleton
    @Named("Api")
    fun provideApi(@Named("Retrofit") builder: Retrofit.Builder): Api {
        return builder.build().create<Api>(Api::class.java)
    }

    companion object {
        val TAG = ApiModule::class.java.simpleName
    }

}

ApiAuthModule with HOST_2 url

@Module
class ApiAuthModule {
    @Provides
    @Singleton
    @Named("InterceptorAuth")
    fun provideInterceptor(): Interceptor {
        return ApiAuthInterceptor()
    }

    @Provides
    @Singleton
    @Named("HttpClientAuth")
    fun provideOkHttpClient(@Named("InterceptorAuth") interceptor: Interceptor): OkHttpClient {
        val builder = OkHttpClient.Builder()
        builder.addInterceptor(interceptor)
        builder.readTimeout(5, TimeUnit.SECONDS)
        builder.connectTimeout(5, TimeUnit.SECONDS)

        if (BuildConfig.DEBUG) {
            val loggingInterceptor = HttpLoggingInterceptor()
            loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            builder.addInterceptor(loggingInterceptor)
        }
        return builder.build()
    }

    @Provides
    @Singleton
    @Named("RetrofitAuth")
    fun provideRetrofitBuilder(@Named("HttpClientAuth") okHttpClient: OkHttpClient): Retrofit.Builder {
        return Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(
                        GsonBuilder()
                                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                                .create()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .baseUrl(BuildConfig.HOST_2)
    }


    @Provides
    @Singleton
    @Named("ApiAuth")
    fun provideApi(@Named("RetrofitAuth") builder: Retrofit.Builder): ApiAuth {
        return builder.build().create<ApiAuth>(ApiAuth::class.java)
    }

    companion object {
        val TAG = ApiModule::class.java.simpleName
    }
}

Usage in AppModule:

  @Provides
  @Singleton
  fun worksRepository(@Named("Api") api: Api, @Named("ApiAuth") api2: ApiAuth): IWorksRepository {
        return WorksRepository(api, api2)
    }
Mikhail
  • 800
  • 8
  • 21