gson Kotlin可抛出函数开始_ARRAY

uz75evzq  于 2022-11-06  发布在  Kotlin
关注(0)|答案(1)|浏览(132)

我试图在我的Kotlin应用程序https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR中从这个api中获取信息,但是当我运行它时,我得到了onFailure:需要开始_ARRAY,但在路径$的第1行第2列却是BEGIN_OBJECT
这是我代码:
类MyData:

package com.example.myapplication
data class MyData(

val BTC: Double,

val EUR: Double,

val USD: Double
)

这是我ApiInterface:

package com.example.myapplication
import retrofit2.Call
import retrofit2.http.GET
interface ApiInterface {
@GET("data/price?fsym=ETH&tsyms=BTC,USD,EUR")

fun getData(): Call<List<MyData>>

}
这是我的主活动.kt

package com.example.myapplication

import android.os.Bundle

import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.lang.StringBuilder

const val Base_Url = "https://min-api.cryptocompare.com/"

class MainActivity : AppCompatActivity() {

private lateinit var txtId: TextView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    getMyData();

    txtId = findViewById(R.id.txtId)

}

private fun getMyData() {
    val retrofitBuilder = Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(Base_Url)
        .build()
        .create(ApiInterface::class.java)

    val retrofitData = retrofitBuilder.getData()

    retrofitData.enqueue(object : Callback<List<MyData>?> {
        override fun onResponse(call: Call<List<MyData>?>, response: Response<List<MyData>?>) {

            val responseBody = response.body()!!

            val myStringBuilder = StringBuilder()
            for (MyData in responseBody) {
                myStringBuilder.append(MyData.BTC)
            }

            txtId.text = myStringBuilder

        }

        override fun onFailure(call: Call<List<MyData>?>, t: Throwable) {
            Log.d("MainActivity", "onFailure: "+t.message)

        }
    })
}

}
然后我得到:com.example.myapplication D/主活动:onFailure:需要开始_ARRAY,但在路径$的第1行第2列却是BEGIN_OBJECT
我真的希望有人能帮我!

k5ifujac

k5ifujac1#

请将ApiInterface中的函数签名更改为:

interface ApiInterface {
    @GET("data/price?fsym=ETH&tsyms=BTC,USD,EUR")
    fun getData(): Call<MyData>
}

您期望的JSON如下所示:

{"BTC":0.07761,"USD":3334.47,"EUR":2923.18}

它既不是数组也不是列表,它是一个对象,所以你需要从函数Call<MyData>中返回一个对象,而不是一个对象列表Call<List<MyData>>
MainActivity中:

retrofitData.enqueue(object : Callback<MyData?> {
    override fun onResponse(call: Call<MyData?>, response: Response<MyData?>) {

        val responseBody = response.body()
        val myStringBuilder = StringBuilder()
        if (responseBody != null) {
            myStringBuilder.append(responseBody.BTC)
        }

        txtId.text = myStringBuilder

    }

    override fun onFailure(call: Call<MyData?>, t: Throwable) {
        Log.d("MainActivity", "onFailure: "+t.message)

    }
})

相关问题