0

Main Activity

   val retrofit = Retrofit.Builder()
        .baseUrl("http://192.168.1.78:3000")
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val api = retrofit.create(ApiService::class.java)

    api.fetchLastNews().enqueue(object : Callback<List<News>>{
        override fun onResponse(call: Call<List<News>>, response: Response<List<News>>) {
            d("exemplo","onResponse")
        }

        override fun onFailure(call: Call<List<News>>, t: Throwable) {
        d("exemplo","onFailure")
        }
    })
}

Interface

 interface ApiService {
@GET("/getultimas")
fun fetchLastNews(): Call<List<News>>
   }

Data class

 package com.example.navigationdrawer

data class News (
val titulo: String
   )

Response from node api

app.get('/getultimas', function (req, res) {
console.log("ultimas noticias");
results = transform(jsonLastNews);
res.json(results);
 });

Its giving error retrofit kotlin expected begin_object but was begin_array

1 Answers1

0

change your data class News to related response from server,to convert json to pojo kotlin check this Create POJO Class for Kotlin

Edit:

because return json from server is { "items": [ { "title": "", "pubDate": "", "link": "", "guid": "", "author": "", "description": "", "content": "", "enclosure": { "link": "", "type": "" }, "categories": [ "" ] } ]} you can change you data class to

data class News {

    @SerializedName("items") //this json key
    @Expose
    val items: List<Item>?= null; /*items is list array because response [] tag*/

}
/* class for List Item */
data class Item {

    @SerializedName("title") //this key value from json
    @Expose
    val title:String; //this key value to variable string in kotlin the type is String

    @SerializedName("pubDate")
    @Expose
    val pubDate:String;

    @SerializedName("link")
    @Expose
    val link:String;
    /* you can add other data*/

}
adi purnama
  • 156
  • 2
  • 6
  • I get this using the convert json2kotlin but what i should do with this? val json = getJson() // your json value here val topic = Gson().fromJson(json, Json4Kotlin_Base::class.java) – Francisco Marques Dec 23 '19 at 18:28
  • You can get json to pojo from this link http://www.jsonschema2pojo.org/ then Converted Kotlin Class to java using Code -> Convert Java File to Kotlin File or CTRL + ALT + SHIFT + K – adi purnama Dec 23 '19 at 18:41
  • i use that but still gives me the same error begin object but was begin array – Francisco Marques Dec 26 '19 at 13:13
  • Can u show me the return from server?for I'm try check the result for valid json format – adi purnama Dec 26 '19 at 16:31
  • { "items": [ { "title": "", "pubDate": "", "link": "", "guid": "", "author": "", "description": "", "content": "", "enclosure": { "link": "", "type": "" }, "categories": [ "" ] } } – Francisco Marques Dec 26 '19 at 22:19
  • after i'm check your json response not have close tag "]" for key items,please check at end of server response there should be a closing tag like `"categories": [ "" ] } ] }` – adi purnama Dec 27 '19 at 02:59
  • Problem was resolved. The problem was that i was sending the item object and i just removed it from the endpoint and it was okay – Francisco Marques Dec 28 '19 at 15:24