【发布时间】:2021-10-16 13:48:57
【问题描述】:
我正在开发新的 android 应用程序,我想在 koin 模块改造中添加 baseurl 和 api_key 但我很困惑
终点: https://api-aws-eu-qa-1.auto1-test.com/v1/car-types/manufacturer?wa_key=coding-puzzle-client-449cc9d&page=0&pageSize=15
API 密钥: coding-puzzle-client-449cc9d
基本网址: https://api-aws-eu-qa-1.auto1-test.com/
我想在我的 modules.kt 文件中正确添加 base_url 和 api_key 以便以后如果您先检查我的 modules.kt,我可以从服务器正确获取数据我正在调用 api_key 然后是基本 url。并按照以下方式设置我的接口,我在接口中调用 get 方法
interface ApiInterface {
@GET("v1/car-types/manufacturer?")
suspend fun getCarResponse(): Call<CarManufactureResponse>
}
在汽车制造商响应下方
data class CarManufactureResponse(
@SerializedName("page")
val page: Int,
@SerializedName("pageSize")
val pageSize: Int,
@SerializedName("totalPageCount")
val totalPageCount: Int,
@SerializedName("mkda")
val mkda: ManufacturerId
)
在制造商 ID 下方
data class ManufacturerId(
@SerializedName("020")
val x020: String,
@SerializedName("040")
val x040: String,
@SerializedName("042")
val x042: String,
@SerializedName("043")
val x043: String,
@SerializedName("057")
val x057: String,
@SerializedName("060")
val x060: String,
@SerializedName("095")
val x095: String,
@SerializedName("107")
val x107: String,
@SerializedName("125")
val x125: String,
@SerializedName("130")
val x130: String,
@SerializedName("141")
val x141: String,
@SerializedName("145")
val x145: String,
@SerializedName("150")
val x150: String,
@SerializedName("157")
val x157: String,
@SerializedName("160")
val x160: String
)
在我想要传递 baseurl 和 api_key 的 Modules.kt koin 设置模块下方
val viewModels = module {
//viewModel { CarViewModel(get()) }
}
val apiModule = module {
single {
val tokenInterceptor = Interceptor { chain ->
val request =
chain
.request()
.newBuilder()
.addHeader(
"API_KEY",Constants.API_KEY
)
.build()
chain.proceed(request)
}
val logInterceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().apply {
this.level = HttpLoggingInterceptor.Level.BODY
}
val okHttpClient =
OkHttpClient.Builder()
.addInterceptor(tokenInterceptor)
.addInterceptor(logInterceptor)
.build()
val retrofit =
Retrofit.Builder()
.client(okHttpClient)
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
retrofit.create(ApiInterface::class.java)
}
}
在常量.kt 下
object Constants {
const val API_KEY = "wa_key=coding-puzzle-client-449cc9d"
const val BASE_URL = "https://api-aws-eu-qa-1.auto1-test.com/"
}
如何在 koin 模块中正确传递 baseurl 和 apiKey 以便正确实现改造逻辑
【问题讨论】:
标签: android retrofit okhttp koin base-url