2017-06-04 82 views
2

我想在Kotlin中執行網絡操作異步。我讀過它,你可以使用async函數做異步。我得到錯誤以下,任何人都可以猜測可能是什麼問題?Kotlin中的未解決的參考異步

未解決的參考:異步

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' 
} 
+0

你不小心(?)發佈您的APP_ID這裏。你應該現在禁用它並獲得一個新的API,除非這個API也需要一個API祕密。 –

+0

@ChristianBrüggemann啊,刪除:) –

+0

@Williams它仍然在歷史上的問題的變化日誌,所以改變它,如果你不想要一些非法使用你的APP_ID –

回答

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