将已解析的数据添加到列表

问题描述

因此,我创建了此函数来为我的列表生成假值:

private fun generateFakeValues(): List<Torrent> {
    val values = mutableListOf<Torrent>()

    val torrent1 = Torrent()
    torrent1.name = "Big Buck Bunny"
    torrent1.downloadSpeed = 0.00
    torrent1.uploadSpeed = 0.00
    torrent1.downloaded = 59.23
    torrent1.length = 263.64

    values.add((torrent1))

    return values
}

它工作正常。现在,我添加了一个Http请求,并想解析数据,但是项目不在列表中:

private fun getTorrents(): List<Torrent> {
    var torrents = mutableListOf<Torrent>()
    val request = Request.Builder()
        .url("...")
        .build()

    client.newCall(request).enqueue(object : Callback {
        override fun onFailure(call: Call,e: IOException) {
            e.printStackTrace()
        }
        override fun onResponse(call: Call,response: Response) {
            response.use {
                if (!response.isSuccessful) {
                    throw IOException("Unexpected code $response")
                }
                torrents = gson.fromJson(response.body!!.string(),mutableListOf<Torrent>()::class.java)
            }
        }
    })
    return torrents
}

我在做什么错了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)