https://kotlinlang.org logo
Title
k

kennyyi

06/04/2017, 11:28 AM
Hi guys, I have a question to use AsyncTask in Kotlin. I want to post a data to server and I created a class which override AsyncTask.
open class PostTask() : AsyncTask<String, Void, Void>() {
        override fun doInBackground(vararg params: String?): Void {
            NetworkModule().getRetrofitClient(URL_TEST).create(PostService::class.java)
                    .postData(params[0])
    }
}
Android studio shows [TYPE_MISMATCH] error for
params[0]
, and if I change
String?
to
String
, the parameter’s type of doInBackground, [NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY] error is happened. 1. How to compare type
Type?
and
Type
? 2. How to write return statement, if return type was
Void
?
v

vyacheslav.gerasimov

06/04/2017, 12:53 PM
Unit
type in Koltin corresponds to
void
type in Java, try this:
open class PostTask() : AsyncTask<String, Unit, Unit>() {
    override fun doInBackground(vararg params: String) {
        NetworkModule().getRetrofitClient(URL_TEST).create(PostService::class.java)
                .postData(params[0])
    }
}
k

kennyyi

06/05/2017, 1:37 AM
Thank a lot, clear every errors