改造 kotlin 预计 begin_object 但 begin_array

Retrofit kotlin expected begin_object but was begin_array

主要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 ApiService {
@GET("/getultimas")
fun fetchLastNews(): Call<List<News>>
   }

数据class

 package com.example.navigationdrawer

data class News (
val titulo: String
   )

来自节点的响应api

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

它给出错误改造 kotlin 预期 begin_object 但 begin_array

更改您的数据class 新闻到来自服务器的相关响应,将json 转换为 pojo kotlin 检查此

编辑:

因为来自服务器的 return json 是 { "items": [ { "title": "", "pubDate": "", "link": "", "guid": "", "author": "", "description": "", "content": "", "enclosure": { "link": "", "type": "" }, "categories": [ "" ] } ]} 您可以将数据 class 更改为

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*/

}