本文介绍了Kotlin改型预期为begin_array,但在第1行为begin_object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个应用程序,其实质是通过按一个按钮,显示了我的api的作者和标题,但是它给出了预期的begin_array错误,但在第1行是begin_object,我尝试了所有操作,没有任何操作可行,有什么问题

I tried to create an application, the essence of which is that by pressing a button, the author and title from my api are displayed, but it gives an error expected begin_array but was begin_object at line 1, I tried everything, nothing works, what is the problem

MainActivity.kt:

MainActivity.kt:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    btn.setOnClickListener {
        sendNetworkRequest()
    }
}
fun sendNetworkRequest(){
    val builder = Retrofit.Builder()
        .baseUrl("http://newsapi.org/v2/")
        .addConverterFactory(GsonConverterFactory.create())
    val retrofit = builder.build()
    val apiInterface: ApiInterface = retrofit.create<ApiInterface>(ApiInterface::class.java)
    val call: retrofit2.Call<UrlImageModel> = apiInterface.getFile()
    call.enqueue(object : Callback<UrlImageModel>{
        override fun onFailure(call: retrofit2.Call<UrlImageModel>, t: Throwable) {
            Log.i("LOL",t.message.toString())
        }
        override fun onResponse(call: retrofit2.Call<UrlImageModel>, response: Response<UrlImageModel>) {
            val statusResponse = response.body()!!
                        result.text = result.text.toString() + '\n' + statusResponse.status


        }
    })
}

ApiInterface.kt:

ApiInterface.kt:

interface ApiInterface {
@GET("everything?q=bitcoin&from=2020-09-12&sortBy=publishedAt&apiKey=1d4f4f812b0b458890db5757fa0d8ce0")
fun getFile(): Call<UrlImageModel>

}

UrlImageModel.kt

UrlImageModel.kt

class UrlImageModel {
@SerializedName("status")
@Expose
var status: String? = null
@SerializedName("totalResults")
@Expose
var totalResult: String? = null
@SerializedName("articles")
@Expose
var articles = ArrayList<Articles>()

}

class Articles{
@Expose
@SerializedName("source")
var source = ArrayList<Source>()
@SerializedName("author")
@Expose
var author: String? = null
@SerializedName("title")
@Expose
var title: String? = null
@SerializedName("description")
@Expose
var description: String? = null
@SerializedName("url")
@Expose
var url: String? = null
@SerializedName("urlToImage")
@Expose
var urlToImage: String? = null
@SerializedName("publishedAt")
@Expose
var publishedAt: String? = null
@SerializedName("content")
@Expose
var content: String? = null

}

class Source{
@SerializedName("id")
@Expose
var id: String? = null
@SerializedName("name")
@Expose
var name: String? = null

}

Json来自我的API(不是全部)在此处输入图片描述

Json from my API(not all)enter image description here

推荐答案

此错误是因为您已在Articles模型类中将 source 声明为Arraylist.但是在实际响应中,它是作为对象来的.

This error is because you have declared source in Articles model class as Arraylist. But in actual response, it is coming as an object.

所以只需修改您的Articles类.

So just modify your Articles class.

替换这些行

@Expose
@SerializedName("source")
var source = ArrayList<Source>()

与这些人

@Expose
@SerializedName("source")
var source :Source? = null
//or
var source = Source()

这篇关于Kotlin改型预期为begin_array,但在第1行为begin_object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 02:35