我试图从货币转换器的API获取一些数据。即使将正确的GET URL传递给response.body">
我使用的货币转换器API从网站RAPIDAPI (https://rapidapi.com/solutionsbynotnull/api/currency-converter18/)
Retrofit Endpoint:
interface CurrencyConverterEndpoint {
@GET("api/v1/convert")
fun getCurrencyConverter(@Query("from") from: String, @Query("to") to: String, @Query("amount") amount: String): Call<ConvertModel>
}
请求结果的模型:
data class ConvertModel (
@SerializedName("from")
val from: String,
@SerializedName("to")
val to: String,
@SerializedName("amountToConvert")
val amountToConvert: Int,
@SerializedName("convertedAmount")
val convertedAmount: Double
)
fun getCurrencyConverter(
from: String,
to: String,
amount: String,
listener: RetrofitListener<ConvertModel>
) {
val getCurrencyConverterCallback = endpoint.getCurrencyConverter(from, to, amount)
getCurrencyConverterCallback.enqueue(object : Callback<ConvertModel> {
override fun onResponse(call: Call<ConvertModel>, response: Response<ConvertModel>) {
if (response.code() == RetrofitConstants.SUCCESS) {
val s = response
}
}
override fun onFailure(call: Call<ConvertModel>, t: Throwable) {
listener.onError(t.message ?: "Erro.")
}
})
}
API的结果
API期望您提供key
和host
。正确的方法如下:
interface CurrencyConverterEndpoint {
@GET("api/v1/convert")
fun getCurrencyConverter(
@Header("X-RapidAPI-Key") token: String = "your API key",
@Header("X-RapidAPI-Host") host: String = "currency-converter18.p.rapidapi.com",
@Query("from") from: String,
@Query("to") to: String,
@Query("amount") amount: String
): Call<ConvertModel>
}