Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 Colum 2 path

Asked

Viewed 23 times

0

I am trying to consume data from an API using retrofit, but whenever I try to connect it returns this error. I realized that an array would be expected but the API is returning an object. But I’m not sure how to make my app receive the object itself.

This is the Json that the API is returning

    {
    "content": [
        {
            "id": 1,
            "name": "Afonso Cláudio",
            "uf": 8,
            "ibge": 3200102,
            "geolocation": "(-20.0778007507324,-41.1260986328125)",
            "location": {
                "x": -20.0778007507324,
                "y": -41.1260986328125
            }
        },
        {
            "id": 2,
            "name": "Água Doce do Norte",
            "uf": 8,
            "ibge": 3200169,
            "geolocation": "(-18.5482006072998,-40.9854011535645)",
            "location": {
                "x": -18.5482006072998,
                "y": -40.9854011535645
            }
        },
        {
            "id": 3,
            "name": "Águia Branca",
            "uf": 8,
            "ibge": 3200136,
            "geolocation": "(-18.9846000671387,-40.7436981201172)",
            "location": {
                "x": -18.9846000671387,
                "y": -40.7436981201172
            }
        },
        {
            "id": 4,
            "name": "Alegre",
            "uf": 8,
            "ibge": 3200201,
            "geolocation": "(-20.757999420166,-41.538200378418)",
            "location": {
                "x": -20.757999420166,
                "y": -41.538200378418
            }
        },
        {
            "id": 5,
            "name": "Alfredo Chaves",
            "uf": 8,
            "ibge": 3200300,
            "geolocation": "(-20.6396007537842,-40.7542991638184)",
            "location": {
                "x": -20.6396007537842,
                "y": -40.7542991638184
            }
        },
        {
            "id": 6,
            "name": "Alto Rio Novo",
            "uf": 8,
            "ibge": 3200359,
            "geolocation": "(-19.0618000030518,-41.0209007263184)",
            "location": {
                "x": -19.0618000030518,
                "y": -41.0209007263184
            }
        },
        {
            "id": 7,
            "name": "Anchieta",
            "uf": 8,
            "ibge": 3200409,
            "geolocation": "(-20.7954998016357,-40.6425018310547)",
            "location": {
                "x": -20.7954998016357,
                "y": -40.6425018310547
            }
        },
        {
            "id": 8,
            "name": "Apiacá",
            "uf": 8,
            "ibge": 3200508,
            "geolocation": "(-21.1522998809814,-41.5693016052246)",
            "location": {
                "x": -21.1522998809814,
                "y": -41.5693016052246
            }
        },
        {
            "id": 9,
            "name": "Aracruz",
            "uf": 8,
            "ibge": 3200607,
            "geolocation": "(-19.8199996948242,-40.2764015197754)",
            "location": {
                "x": -19.8199996948242,
                "y": -40.2764015197754
            }
        },
        {
            "id": 10,
            "name": "Atilio Vivacqua",
            "uf": 8,
            "ibge": 3200706,
            "geolocation": "(-20.9130001068115,-41.198600769043)",
            "location": {
                "x": -20.9130001068115,
                "y": -41.198600769043
            }
        },
        {
            "id": 11,
            "name": "Baixo Guandu",
            "uf": 8,
            "ibge": 3200805,
            "geolocation": "(-19.5212993621826,-41.0108985900879)",
            "location": {
                "x": -19.5212993621826,
                "y": -41.0108985900879
            }
        },
        {
            "id": 12,
            "name": "Barra de São Francisco",
            "uf": 8,
            "ibge": 3200904,
            "geolocation": "(-18.7548007965088,-40.8964996337891)",
            "location": {
                "x": -18.7548007965088,
                "y": -40.8964996337891
            }
        },
        {
            "id": 13,
            "name": "Boa Esperança",
            "uf": 8,
            "ibge": 3201001,
            "geolocation": "(-18.5394992828369,-40.3025016784668)",
            "location": {
                "x": -18.5394992828369,
                "y": -40.3025016784668
            }
        },
        {
            "id": 14,
            "name": "Bom Jesus do Norte",
            "uf": 8,
            "ibge": 3201100,
            "geolocation": "(-21.1173000335693,-41.6730995178223)",
            "location": {
                "x": -21.1173000335693,
                "y": -41.6730995178223
            }
        },
        {
            "id": 15,
            "name": "Brejetuba",
            "uf": 8,
            "ibge": 3201159,
            "geolocation": "(-20.1394996643066,-41.2953987121582)",
            "location": {
                "x": -20.1394996643066,
                "y": -41.2953987121582
            }
        },
        {
            "id": 16,
            "name": "Cachoeiro de Itapemirim",
            "uf": 8,
            "ibge": 3201209,
            "geolocation": "(-20.8462009429932,-41.119800567627)",
            "location": {
                "x": -20.8462009429932,
                "y": -41.119800567627
            }
        },
        {
            "id": 17,
            "name": "Cariacica",
            "uf": 8,
            "ibge": 3201308,
            "geolocation": "(-20.2632007598877,-40.4165000915527)",
            "location": {
                "x": -20.2632007598877,
                "y": -40.4165000915527
            }
        },
        {
            "id": 18,
            "name": "Castelo",
            "uf": 8,
            "ibge": 3201407,
            "geolocation": "(-20.6033000946045,-41.2030982971191)",
            "location": {
                "x": -20.6033000946045,
                "y": -41.2030982971191
            }
        },
        {
            "id": 19,
            "name": "Colatina",
            "uf": 8,
            "ibge": 3201506,
            "geolocation": "(-19.5492992401123,-40.6268997192383)",
            "location": {
                "x": -19.5492992401123,
                "y": -40.6268997192383
            }
        },
        {
            "id": 20,
            "name": "Conceição da Barra",
            "uf": 8,
            "ibge": 3201605,
            "geolocation": "(-18.5883007049561,-39.736198425293)",
            "location": {
                "x": -18.5883007049561,
                "y": -39.736198425293
            }
        }
    ],
    "pageable": {
        "sort": {
            "sorted": false,
            "unsorted": true,
            "empty": true
        },
        "pageNumber": 0,
        "pageSize": 20,
        "offset": 0,
        "unpaged": false,
        "paged": true
    },
    "totalPages": 280,
    "totalElements": 5595,
    "last": false,
    "sort": {
        "sorted": false,
        "unsorted": true,
        "empty": true
    },
    "numberOfElements": 20,
    "first": true,
    "size": 20,
    "number": 0,
    "empty": false
}

this is the date class:

data class City (

        @SerializedName("Id")
        val id: Long,
        @SerializedName("name")
        val name: String,
        @SerializedName("uf")
        val uf: Int,
        @SerializedName("ibge")
        val ibge: Int,
        @SerializedName("geolocation")
        val geolocation: String,
        @SerializedName("location")
        val location: Point,
)

This is the interface :

    interface CityService {
    @GET("cities")
    fun getCity() : Call<List<City>>    
}

Esta a Mainactivity :

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getData()
    }

    fun getData() {
        val retrofitClient = RetrofitConfig
                .getRetrofitInstance("https://afternoon-dusk-77298.herokuapp.com/")

        val cityService = retrofitClient.create(CityService::class.java)
        val callback = cityService.getCity()

        callback.enqueue(object : Callback<List<City>> {
            override fun onFailure(call: Call<List<City>>, t: Throwable) {
                Toast.makeText(baseContext, t.message, Toast.LENGTH_SHORT).show()
            }

            override fun onResponse(call: Call<List<City>>, response: Response<List<City>>) {
                response.body()?.forEach {
                    val textView = findViewById<TextView>(R.id.textView)
                    textView.text = textView.text.toString().plus(it.id)

                   
                }
            }
        })
    }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.