Home > front end >  The Retrofit GET method isn't getting the result from the API
The Retrofit GET method isn't getting the result from the API

Time:12-02

I'm trying to GET some datas FROM an API of Currency Converter. Even passing the right GET URL the "response.body" in coming with null values.

I'm using the Currency Converter API from the website RAPIDAPI (https://rapidapi.com/solutionsbynotnull/api/currency-converter18/)

The Retrofit Endpoint:

interface CurrencyConverterEndpoint {

    @GET("api/v1/convert")
    fun getCurrencyConverter(@Query("from") from: String, @Query("to") to: String, @Query("amount") amount: String): Call<ConvertModel>

}

The Model of the result from the request:

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.")
        }

    })
}

The result from the API

CodePudding user response:

The API expect key and host from you. The proper method should be like below:

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>

}
  • Related