相关疑难解决方法(0)

Kotlin中未解析的引用异步

我正在尝试执行网络操作异步Kotlin.我读了它你可以使用async函数做异步.我收到以下错误,有人可以猜出可能是什么问题吗?

未解决的参考:async

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

    async() {
        val forecastWeather = ForecastRequest("302015").execute()
        Log.d("Test", forecastWeather.toString())
    }
}
Run Code Online (Sandbox Code Playgroud)

ForecastRequest.kt

class ForecastRequest(val zipcode: String) {
    companion object {
        private val APP_ID = "XYZ"
        private val URL = "http://api.openweathermap.org/data/2.5/" +
                    "forecast/daily?mode=json&units=metric&cnt=7"
        private val COMPLETE_URL = "$URL&APPID=$APP_ID&q="
    }

    fun execute(): ForecastResponse {
        val forecastJsonStr = java.net.URL(COMPLETE_URL + zipcode).readText()
        return Gson().fromJson(forecastJsonStr, ForecastResponse::class.java)
    }
}
Run Code Online (Sandbox Code Playgroud)

顶级build.gradle

// Top-level build file where you can add configuration options common to all …
Run Code Online (Sandbox Code Playgroud)

android kotlin

11
推荐指数
2
解决办法
5989
查看次数

标签 统计

android ×1

kotlin ×1