为什么改造在 mainactivity 中给出错误?

why retrofit giving an error in mainactivity?

我正在开发货币应用程序,但是当我尝试在 mainactivity.kt 中实现网络调用时出现错误我不明白我到底在哪里出错

低于我的MainActivity.kt

我在哪里实现了改造网络调用

class MainActivity : AppCompatActivity() {


    private var listUsers: MutableList<Quotes> = mutableListOf<Quotes>()
    private var adapter: CurrenciesAdapter? = null


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

        listUsers = mutableListOf()

        recycler_main.layoutManager = LinearLayoutManager(this@MainActivity)
        adapter = CurrenciesAdapter(
            this,
            listUsers
        )
        recycler_main.adapter = adapter

        if (isInternetAvailable()) {
            getUsersData()
        }

    }

    private fun getUsersData() {

        showProgressBar()

        var apiInterface: CurrenciesResponse = CurrencyClient().getApiClient()!!.create(
            CurrenciesResponse::class.java
        )

        apiInterface.getCurrencies().enqueue(object : Callback <MutableList<Quotes>> {
            override fun onResponse(
                call: Call<MutableList<Quotes>>,
                response: Response<MutableList<Quotes>>
            ) {
                hideProgressBar()
                val currencyResponse = response.body()
                listUsers.clear()
                currencyResponse.let { listUsers.addAll(it)}
                adapter?.notifyDataSetChanged()
            }

            override fun onFailure(call: Call<MutableList<Quotes>>, t: Throwable) {
                hideProgressBar()
                Log.e("error", t.localizedMessage)
            }
        })
    }
}

低于我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_main"
        android:layout_width="392dp"
        android:layout_height="736dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

低于我的适配器 class

class CurrenciesAdapter(private val context: Context, private var list: MutableList<Quotes>) : RecyclerView.Adapter<CurrenciesAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val inflater = LayoutInflater.from(context)
        val view: View = inflater.inflate(R.layout.currency_layout,parent,false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val user = list.get(position)
        holder.name?.text = user.USDAED.toString()
        holder.info1?.text = user?.USDAFN.toString()
        holder.info2?.text = user?.USDALL.toString()
        val addressObj = user.USDANG
        holder.address?.text = user.USDAOA.toString()
    }

    class MyViewHolder(var view: View) : RecyclerView.ViewHolder(view){

        var name: TextView? = null
        var info1: TextView? = null
        var info2: TextView? = null
        var address: TextView? = null

        init {
            name = view.findViewById(R.id.txt_user_name)
            info1 = view.findViewById(R.id.txt_user_info1)
            info2 = view.findViewById(R.id.txt_user_info2)
            address = view.findViewById(R.id.txt_user_address)
        }

    }

}

低于currency_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="15dp"
    android:layout_marginEnd="15dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <TextView
            android:id="@+id/txt_user_name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txt_user_info1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txt_user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/txt_user_info2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txt_user_info1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/txt_user_address"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txt_user_info2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

CurrenciesResponse 界面下方

interface CurrenciesResponse {

    @GET("live?access_key=1f72464a836ff6084b8b1780e3feadf9")
    fun getCurrencies(): Call<CurrencyResponse>

}

低于 baseUrl 实现

class CurrencyClient {


    private val BASE_URL = "http://api.currencylayer.com/"
    public var retrofit: Retrofit? = null

    public fun getApiClient(): Retrofit? {
        if (retrofit == null) {
            retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build()
        }
        return retrofit
    }
}

低于CurrencyResponse.kt

data class CurrencyResponse(
    val privacy: String,
    val quotes: Quotes,
    val source: String,
    val success: Boolean,
    val terms: String,
    val timestamp: Int
)

低于Quotes.kt

data class Quotes(
    val USDAED: Double,
    val USDAFN: Double,
    val USDALL: Double,
    val USDAMD: Double,
    val USDANG: Double,
    val USDAOA: Double,
    val USDARS: Double,
    val USDAUD: Double,
    val USDAWG: Double,
    val USDAZN: Double,
    val USDBAM: Double,
    val USDBBD: Double,
    val USDBDT: Double,
    val USDBGN: Double,
    val USDBHD: Double,
    val USDBIF: Int,
    val USDBMD: Int,
    val USDBND: Double,
    val USDBOB: Double,
    val USDBRL: Double,
    val USDBSD: Double,
    val USDBTC: Double,
    val USDBTN: Double,
    val USDBWP: Double,
    val USDBYN: Double,
    val USDBYR: Int,
    val USDBZD: Double,
    val USDCAD: Double,
    val USDCDF: Double,
    val USDCHF: Double,
    val USDCLF: Double,
    val USDCLP: Double,
    val USDCNY: Double,
    val USDCOP: Double,
    val USDCRC: Double,
    val USDCUC: Int,
    val USDCUP: Double,
    val USDCVE: Double,
    val USDCZK: Double,
    val USDDJF: Double,
    val USDDKK: Double,
    val USDDOP: Double,
    val USDDZD: Double,
    val USDEGP: Double,
    val USDERN: Double,
    val USDETB: Double,
    val USDEUR: Double,
    val USDFJD: Double,
    val USDFKP: Double,
    val USDGBP: Double,
    val USDGEL: Double,
    val USDGGP: Double,
    val USDGHS: Double,
    val USDGIP: Double,
    val USDGMD: Double,
    val USDGNF: Double,
    val USDGTQ: Double,
    val USDGYD: Double,
    val USDHKD: Double,
    val USDHNL: Double,
    val USDHRK: Double,
    val USDHTG: Double,
    val USDHUF: Double,
    val USDIDR: Double,
    val USDILS: Double,
    val USDIMP: Double,
    val USDINR: Double,
    val USDIQD: Double,
    val USDIRR: Double,
    val USDISK: Double,
    val USDJEP: Double,
    val USDJMD: Double,
    val USDJOD: Double,
    val USDJPY: Double,
    val USDKES: Double,
    val USDKGS: Double,
    val USDKHR: Double,
    val USDKMF: Double,
    val USDKPW: Double,
    val USDKRW: Double,
    val USDKWD: Double,
    val USDKYD: Double,
    val USDKZT: Double,
    val USDLAK: Double,
    val USDLBP: Double,
    val USDLKR: Double,
    val USDLRD: Double,
    val USDLSL: Double,
    val USDLTL: Double,
    val USDLVL: Double,
    val USDLYD: Double,
    val USDMAD: Double,
    val USDMDL: Double,
    val USDMGA: Double,
    val USDMKD: Double,
    val USDMMK: Double,
    val USDMNT: Double,
    val USDMOP: Double,
    val USDMRO: Double,
    val USDMUR: Double,
    val USDMVR: Double,
    val USDMWK: Double,
    val USDMXN: Double,
    val USDMYR: Double,
    val USDMZN: Double,
    val USDNAD: Double,
    val USDNGN: Double,
    val USDNIO: Double,
    val USDNOK: Double,
    val USDNPR: Double,
    val USDNZD: Double,
    val USDOMR: Double,
    val USDPAB: Double,
    val USDPEN: Double,
    val USDPGK: Double,
    val USDPHP: Double,
    val USDPKR: Double,
    val USDPLN: Double,
    val USDPYG: Double,
    val USDQAR: Double,
    val USDRON: Double,
    val USDRSD: Double,
    val USDRUB: Double,
    val USDRWF: Int,
    val USDSAR: Double,
    val USDSBD: Double,
    val USDSCR: Double,
    val USDSDG: Double,
    val USDSEK: Double,
    val USDSGD: Double,
    val USDSHP: Double,
    val USDSLL: Double,
    val USDSOS: Double,
    val USDSRD: Double,
    val USDSTD: Double,
    val USDSVC: Double,
    val USDSYP: Double,
    val USDSZL: Double,
    val USDTHB: Double,
    val USDTJS: Double,
    val USDTMT: Double,
    val USDTND: Double,
    val USDTOP: Double,
    val USDTRY: Double,
    val USDTTD: Double,
    val USDTWD: Double,
    val USDTZS: Double,
    val USDUAH: Double,
    val USDUGX: Double,
    val USDUSD: Int,
    val USDUYU: Double,
    val USDUZS: Double,
    val USDVEF: Double,
    val USDVND: Int,
    val USDVUV: Double,
    val USDWST: Double,
    val USDXAF: Double,
    val USDXAG: Double,
    val USDXAU: Double,
    val USDXCD: Double,
    val USDXDR: Double,
    val USDXOF: Double,
    val USDXPF: Double,
    val USDYER: Double,
    val USDZAR: Double,
    val USDZMK: Double,
    val USDZMW: Double,
    val USDZWL: Double
)

我想知道我在哪里犯了错误我必须做什么才能在 MainActivity.kt

中正确实施网络

我只是手动点击 API。

看来问题出在接收类型MutableList<Quotes>

apiInterface.getCurrencies().enqueue(object : Callback <CurrencyResponse> {
            override fun onResponse(
                call: Call<CurrencyResponse>,
                response: Response<CurrencyResponse>
            ) {
                hideProgressBar()
              //your logic goes here
            }

            override fun onFailure(call: Call<CurrencyResponse>, t: Throwable) {
                hideProgressBar()
                Log.e("error", t.localizedMessage)
            }
        })

data class CurrencyResponse(
    val privacy: String,
    val quotes: Quotes,
    val source: String,
    val success: Boolean,
    val terms: String,
    val timestamp: Int
)

val timestamp: Int,你应该这样做

data class CurrencyResponse(
        val privacy: String,
        val quotes: Quotes,
        val source: String,
        val success: Boolean,
        val terms: String,
        val timestamp: Long
    )

由于您的 API 结果为 "timestamp": 1610813346,建议将其保存在 Long 类型的变量中。就像我们对 System.currentTimeMillis().

所做的那样