Hi guys, I have a question to use AsyncTask in Kot...
# android
k
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.
Copy code
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
Unit
type in Koltin corresponds to
void
type in Java, try this:
Copy code
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
Thank a lot, clear every errors