簡體   English   中英

Kotlin中未解析的引用異步

[英]Unresolved reference async in 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())
    }
}

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

頂級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
}

模塊級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())
    }
}

kotlinx.couroutines中可以使用async

確保在gradle文件中添加了依賴項:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'

還要確保在諸如GlobalScope類的協程scrope上調用異步。

 val deferredResult = GlobalScope.async {

            }

不同的Kotlin庫可以有不同的async實現,可以執行不同的操作。

如果你想從核心庫中獲得通用的async-await函數 ,請確保你對kotlinx.coroutines有依賴性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM