【问题标题】:Unresolved reference async in KotlinKotlin 中未解决的异步引用
【发布时间】:2017-11-05 08:46:58
【问题描述】:

我正在尝试在Kotlin 中执行异步网络操作。我读到你可以使用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'
}

【问题讨论】:

  • 您不小心 (?) 在此处发布了您的 APP_ID。您现在应该禁用它并获得一个新的,除非此 API 还需要 API 密钥。
  • @ChristianBrüggemann 啊,已删除 :)
  • @Williams 它仍然在问题的更改日志中,所以如果您不想非法使用您的 APP_ID,请更改它

标签: android kotlin


【解决方案1】:

asynckotlinx.couroutines 中可用

确保您在 gradle 文件中添加了依赖项:

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

还要确保在诸如GlobalScope 之类的协程范围内调用异步。

 val deferredResult = GlobalScope.async {

            }

【讨论】:

    【解决方案2】:

    我看到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())
        }
    }
    

    【讨论】:

      【解决方案3】:

      不同的 Kotlin 库可以有不同的 async 实现来做不同的事情。

      如果您想要核心库中的 general async-await function,请确保您依赖于 kotlinx.coroutines

      【讨论】:

        猜你喜欢
        • 2019-06-14
        • 1970-01-01
        • 2019-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多