在我的应用程序中,我开始使用 Hilt 作为 DI。所以我创建了一个类来像这样在我的存储库中提供改造
@InstallIn(ApplicationComponent::class)
object RetrofitModule {
var baseUrl = "https://my.fancy.api"
@Singleton
@Provides
fun providesRetrofitClient(): Retrofit {
return Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(providesOkHttpClient())
.build()
}
@Singleton
@Provides
fun providesOkHttpClient(): OkHttpClient {
val okHttpClientBuilder = OkHttpClient.Builder()
val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
okHttpClientBuilder.addInterceptor(loggingInterceptor)
return okHttpClientBuilder.build()
}
@Singleton
@Provides
fun providesJokeGeneratorService(retrofit: Retrofit): FancyApiService {
return retrofit.create(FancyApiService::class.java)
}
我的问题,如何更改 url 以在带有 Hilt 的 Mockwebserver 中使用它?