Skip to content
Advertisement

Handle Response Kotlin Retrofit

I’m new to Kotlin and android development. Have been struggling to get my retrofit api to work.
But have found a way to do it after some search on SO. I get the data response now, but I don’t know how to “separate” it, so that i can work around with it.

this is my json response:

"data": [
    {
        "alpha2Code": "PT",
        "name": "Portugal",
        "prefixCode": null,
        "id": "9ba94c99-7362-47c2-f31f-08d87a662921",
        "active": true,
        "created": "2020-10-27T10:50:46.895831"
    }

and my model class

data class Country (
    @SerializedName("alpha2Code")
    val alpha2Code: String?,
    @SerializedName("name")
    val name: String?,
    @SerializedName("id")
    val id: String?,
    @SerializedName("active")
    val active: Boolean,
    @SerializedName("created")
    val created: String?
): Serializable


class Countrys {
    var countrys: List<Country> = emptyList()
}

and finally my get data function

fun getDataCountry() {
    val call: Call<Countrys> = ApiClient.getClient.getCountries()

    call.enqueue(object : Callback<Countrys> {
        override fun onResponse(call: Call<Countrys>?, response: Response<Countrys>?) {
            // val carResponse = response.body()
            val body = response?.body()
            Log.e("dadosApi2","retorno response: " + body)
        }

        override fun onFailure(call: Call<Countrys>?, t: Throwable) {
            Log.e("dadosApiError","erro no retorno " + t.message)
        }
    })
}

I get the response, but I don’t know how to unfold the data, so that I can for example add all country names to an ArrayList.

I have tried doing this without the class Countrys, using <List> or Arraylist but i get the error on my response:

E/dadosApiError: erro no retorno Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

fun getDataCountry() {
    val call: Call<ArrayList<Country>> = ApiClient.getClient.getCountries()
    call.enqueue(object : Callback<ArrayList<Country>> {

        override fun onResponse(call: Call<ArrayList<Country>>?, response: Response<ArrayList<Country>>?) {
            // val carResponse = response.body()
            val body = response?.body()

            Log.e("dadosApi2","retorno response: " + body)

        }

        override fun onFailure(call: Call<ArrayList<Country>>?, t: Throwable) {
            Log.e("dadosApiError","erro no retorno " + t.message)
        }

    })
}

I have also tried previously with List

Advertisement

Answer

you will need to change Countrys class into a data class and add the SerializedName data for the object countrys like this

data class Countrys(@SerializedName("data")var countrys: List<Country>)

then you can access your data by using this

var countryNames = mutableListOf<String>()
for (country in response?.body().countrys){
  countryNames.add(country.name)
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement