@Singleton
@Provides
fun providesHttpClient(
logging: HttpLoggingInterceptor,
@ApplicationContext context: Context,
authenticator: TokenAuthenticator
): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(logging)
.retryOnConnectionFailure(false)
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.certificatePinner(CertificatePinner.Builder().add("com.example.domain",CONSTANT.KEY).build())
.authenticator(authenticator)
.build()
}
@单例 @提供 有趣的 providesConvertorFactory(): GsonConverterFactory { 返回 GsonConverterFactory.create() }
@Singleton
@Provides
fun provideRetrofit(
okHttpClient: OkHttpClient,
gsonConverterFactory: GsonConverterFactory
): Retrofit {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(gsonConverterFactory)
.build()
}
密钥将从服务器获取,最初是空的。
如何更新 CertificatePinner 值