kotlin 改造预计 begin_array 但在第 1 行 begin_object
kotlin retrofit expected begin_array but was begin_object at line 1
我试图创建一个应用程序,其本质是通过按下按钮,显示我 api 的作者和标题,但它给出了预期的错误 begin_array 但begin_object 在第 1 行,我尝试了所有方法,没有任何效果,问题是什么
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:
interface ApiInterface {
@GET("everything?q=bitcoin&from=2020-09-12&sortBy=publishedAt&apiKey=1d4f4f812b0b458890db5757fa0d8ce0")
fun getFile(): Call<UrlImageModel>
}
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(不是全部)
enter image description here
尝试将文章 Class 更改为:
class Articles{
@Expose
@SerializedName("source")
var source = 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 声明为 Arraylist。但在实际响应中,它是以对象的形式出现的。
所以只需修改您的文章 class。
替换这些行
@Expose
@SerializedName("source")
var source = ArrayList<Source>()
这些
@Expose
@SerializedName("source")
var source :Source? = null
//or
var source = Source()
我试图创建一个应用程序,其本质是通过按下按钮,显示我 api 的作者和标题,但它给出了预期的错误 begin_array 但begin_object 在第 1 行,我尝试了所有方法,没有任何效果,问题是什么
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:
interface ApiInterface {
@GET("everything?q=bitcoin&from=2020-09-12&sortBy=publishedAt&apiKey=1d4f4f812b0b458890db5757fa0d8ce0")
fun getFile(): Call<UrlImageModel>
}
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(不是全部) enter image description here
尝试将文章 Class 更改为:
class Articles{
@Expose
@SerializedName("source")
var source = 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 声明为 Arraylist。但在实际响应中,它是以对象的形式出现的。
所以只需修改您的文章 class。
替换这些行
@Expose
@SerializedName("source")
var source = ArrayList<Source>()
这些
@Expose
@SerializedName("source")
var source :Source? = null
//or
var source = Source()