branch master is based on:
org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6
branch lifecycle is based on:
androidx.lifecycle:lifecycle-runtime-ktx:2.3.0-alpha03
repositories {
maven { url 'https://jitpack.io' }
}
...
dependencies {
implementation 'com.github.yfbx:coroutine-ext:1.0.1'
}
- Define api with keyword
suspend
and the return type is just what you want
interface UserApi {
@FormUrlEncoded
@POST("users/login")
suspend fun login(@Field("account") mobile: String, @Field("password") password: String): User
}
BaseActivity
extendsCoroutineScope
abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
//cancel coroutine task when destroyed
override fun onDestroy() {
cancel()
super.onDestroy()
}
}
- Use extention in subtype of
CoroutineScope
private fun login() = network {
val user = Net.create<UserApi>().login("18888888888", "123456")
infoTxt.append("Response:${user}")
}
private fun login() {
loading {
val user = Net.create<UserApi>().login("18888888888", "123456")
infoTxt.append("Response:${user}")
}.onError { code, msg ->
infoTxt.append("code:$code , message:$msg")
}
}