1. 将java中的回调接口封装成挂起函数
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
suspend fun Call.await(): String = suspendCoroutine { block ->
enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
block.resumeWithException(e)
}
override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) {
block.resume(response.body()!!.string())
}
}
})
}
val client: OkHttpClient = OkHttpClient.Builder() .build()
val request: Request = Request.Builder()
.get()
.url("http://xxx")
.build()
Glo