【问题标题】:Can not change retrofit base url more than once with reflection不能通过反射多次更改改造基础 url
【发布时间】:2018-10-17 11:51:17
【问题描述】:

我正在使用反射来动态更改改造基础网址,但我可以在活动中更改一次,之后我无法更改它。为什么当我尝试多次更改时它不起作用。

Set dynamic base url using Retrofit 2.0 and Dagger 2

我不知道域我只知道端点,我从用户那里得到域地址。

【问题讨论】:

  • 你尝试过 Retrofit 的 @Url 注释吗?
  • 我想我必须为每个请求使用它。我想在中心位置更改基本网址

标签: android reflection retrofit retrofit2


【解决方案1】:

只需对不同的 URL 使用不同的模块和 api。然后使用@Named 注解确定正确的api。 带有 HOST url 的 API 模块

@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
    }

}

带有 HOST_2 url 的 ApiAuthModule

@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
    }
}

在 AppModule 中的使用:

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

【讨论】:

  • 对此我很抱歉,但我从用户那里得到了网址。所以我不知道域我只知道端点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 2019-04-14
相关资源
最近更新 更多