我正在try 在Kotlin中执行异步网络操作.我看过了,你可以用async函数做异步.我快出错了,有没有人能猜出是什么问题?

Unresolved reference: 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())
    }
}

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)
    }
}

Top level build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Module level build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.williams.thirddemo"
        minSdkVersion 23
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.code.gson:gson:2.8.1'
}

推荐答案

我看到async函数在anko库中可用,而不是Kotlin库本身.https://github.com/Kotlin/anko

我通过在build中添加这个依赖项来解决这个问题.gradle compile "org.jetbrains.anko:anko-commons:0.10.1",因为这在Kotlin本身不可用.

我看到async已经过时了,图书馆建议用doAsync代替.

doAsync {
    val forecastWeather = ForecastRequest("302015").execute()
    uiThread {
        Log.d("Test", forecastWeather.toString())
    }
}

Kotlin相关问答推荐

带有Spring Boot和Kotline的可嵌入实体

用Quarkus和Reactor重写异步过滤器中的数据流

限制通用Kotlin枚举为特定类型

为什么 <= 可以应用于 Int 和 Long,而 == 不能?

如何在 Spring Boot 3 中为内部类提供运行时提示

Kotlin 协程按顺序执行,但仅在生产机器上执行

Kotlin 函数有 2 个参数用于对 Map 或 List 进行排序

为什么 Kotlin 中的 Double 和 Long 类型不推荐直接转换为 Char?

如何在 Kotlin 中将with代码转换为完整代码?

Mixin 在 Jackson 中添加 defaultImpl 不起作用

is return inside function definition 也是 kotlin 中的表达式

Kotlin 日期格式从一种更改为另一种

添加 Kapt 插件后 - 执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障

Kotlin 和 Java 字符串拆分与正则表达式的区别

Kotlin 单元测试 - 如何模拟 Companion 对象的组件?

将 @Component.Builder 与构造函数参数一起使用

在Kotlin中传递并使用函数作为构造函数参数

Kotlin中具有多个参数的绑定适配器

如果作为 RxJava Observable 提供,Kotlin 密封类子类需要强制转换为基类

如何在 Gradle Kotlin DSL 中使用来自 gradle.properties 的插件版本?