带有Kotlin的Android中的AsyncTask [英] AsyncTask in Android with Kotlin

查看:270
本文介绍了带有Kotlin的Android中的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Kotlin在Android中进行API调用?

How to make an API call in Android with Kotlin?

我听说过 Anko .但是我想使用Kotlin提供的方法,例如在Android中,我们有Asynctask用于后台操作.

I have heard of Anko . But I want to use methods provided by Kotlin like in Android we have Asynctask for background operations.

推荐答案

AsyncTask是Android API ,不是Java或Kotlin提供的语言功能 .如果需要,您可以像这样使用它们:

AsyncTask is an Android API, not a language feature that is provided by Java nor Kotlin. You can just use them like this if you want:

class someTask() : AsyncTask<Void, Void, String>() {
    override fun doInBackground(vararg params: Void?): String? {
        // ...
    }

    override fun onPreExecute() {
        super.onPreExecute()
        // ...
    }

    override fun onPostExecute(result: String?) {
        super.onPostExecute(result)
        // ...
    }
}

Anko的doAsync并不是Kotlin真正提供的,因为Anko是一个使用Kotlin的语言功能来简化长代码的库.检查这里:

Anko's doAsync is not really 'provided' by Kotlin, since Anko is a library that uses language features from Kotlin to simplify long codes. Check here:

如果您使用Anko,则您的代码将与此类似:

If you use Anko your code will be similar to this:

doAsync {
    // ...
}

这篇关于带有Kotlin的Android中的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆