【问题标题】:kotlin retrofit expected begin_array but was begin_object at line 1kotlin 改造预期 begin_array 但在第 1 行是 begin_object
【发布时间】:2021-01-27 06:58:15
【问题描述】:

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

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

}

来自我的 API 的 JSON(不是全部) enter image description here

【问题讨论】:

    标签: android arrays api kotlin retrofit


    【解决方案1】:

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

    所以只需修改您的 Articles 类。

    替换这些行

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

    这些

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

    【讨论】:

    • 现在问题 - 预期为字符串,但在第 1 行第 59 列路径处是 BEGIN_OBJECT
    • 这是因为您已经声明了一些键来返回字符串值。但实际上它返回的是一个值对象。你能分享你的日志吗?如果它对你有用,也接受答案。
    • 2020-10-13 08:49:38.768 4649-4649/com.example.retrofitwork D/NetworkSecurityConfig:未指定网络安全配置,使用平台默认值 2020-10-13 08:49:39.308 4649-4649/com.example.retrofitwork I/LOL:java.lang.IllegalStateException:应为字符串,但在第 1 行第 59 列路径 $.articles[0].source 处为 BEGIN_OBJECT
    • 您能检查一下您的回复吗?您实际上在源代码中得到了什么?此错误表示您将 source 作为字符串获取。或者你可以和我分享你的响应 json
    • 覆盖 fun onResponse(call: retrofit2.Call, response: Response) { val statusResponse = response.body()!! result.text = result.text.toString() + '\n' + statusResponse.articles[1].author + '\n' + statusResponse.articles[1].title] }
    【解决方案2】:

    尝试将 Articles 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
    

    【讨论】:

    • 我试过这个 var 源? = null 因为 var source = Source() 不起作用及其问题 预期为字符串,但在第 1 行第 59 列路径为 BEGIN_OBJECT
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    相关资源
    最近更新 更多