小编Ham*_*di 的帖子

无法在 Kotlin 中更改全局变量值

我正在更改方法内的全局变量值并尝试稍后返回它。

在 FetchData.kt(被调用的类)

var homeFeed: HomeFeed? = null     // the variable that needs to be changed 

fun execute() {


    val client = OkHttpClient()
    val url =
        "..."
    val request = Request.Builder().url(url).build()

    val res = client.newCall(request).enqueue(object : Callback {

        override fun onFailure(call: Call, e: IOException) {

            e.printStackTrace()

        }

        override fun onResponse(call: Call, response: Response) {

            val ch = response?.body?.string()

            val gson = GsonBuilder().create()
            homeFeed= gson.fromJson(ch, HomeFeed::class.java)  // where the change happens

        }

    })

}

   fun GetData(): HomeFeed? {

       return …
Run Code Online (Sandbox Code Playgroud)

android kotlin

2
推荐指数
1
解决办法
1594
查看次数

标签 统计

android ×1

kotlin ×1