如何在协同程序中进行同步呼叫

时间:2019-10-28 16:03:13

标签: android coroutine kotlin-coroutines

我想使我的网络请求同步,因为第二个请求的输入来自第一个请求的输出。

override fun onCreate(savedInstanceState: Bundle?) {

retrofit1 =Retrofit.Builder()
        .baseUrl("https://jsonplaceholder.typicode.com/").addConverterFactory(GsonConverterFactory.create()).build()

retrofit2 =Retrofit.Builder()
        .baseUrl("https://samples.openweathermap.org/").addConverterFactory(GsonConverterFactory.create()).build()

button.setOnClickListener { view ->
CoroutineScope(IO).launch {
fakeApiRequest()
}}

在我的fakeApiRequest()中,我正在发出两个网络请求。

private suspend fun fakeApiRequest() {
val result1 :Geo?= getResult1FromApi()
val result2: Long? = getResult2FromApi(result1)}

由于这是一个异步调用,因为传递的参数为null,所以我的getResult2FromApi(result1)方法中出现了Null Pointer Exception。

为了解决此问题,我不得不在第一次通话中添加delay(1500)。

private suspend fun getResult1FromApi(): Geo? {
val service:CallService = retrofit1!!.create(CallService::class.java)
val call = service.getUsers()
call.enqueue(object : Callback<List<User>> {
override fun onResponse(call: Call<List<User>>, response: Response<List<User>>) {
g = users.get(0).address.geo
}
override fun onFailure(call: Call<List<User>>, t: Throwable) {

}
})
delay(1500)
return g
}

-----------------------------------------------------------

private suspend fun getResult2FromApi(result1: Geo?): Long? {
val service2:CallService = retrofit2!!.create(CallService::class.java)
val call2 = service2.getWeather(result1?.lat!!, result1.lng,"b6907d289e10d714a6e88b30761fae22")
call2.enqueue(object : Callback<WeatherData> {
override fun onResponse(call: Call<WeatherData>, response: Response<WeatherData>) {

}
override fun onFailure(call: Call<WeatherData>, t: Throwable) {
}
})
return dt
}

无论如何我都可以使它同步,这样我就不必经过任何延迟时间。

1 个答案:

答案 0 :(得分:3)

您尚未正确实现可挂起功能。您必须使用suspendCoroutine

suspend fun getResult1FromApi(): Geo? = suspendCoroutine { continuation ->
    val service = retrofit1!!.create(CallService::class.java)
    service.getUsers().enqueue(object : Callback<List<User>> {
        override fun onResponse(call: Call<List<User>>, response: Response<List<User>>) {
            continuation.resume(response.result.getOrNull(0)?.address?.geo)
        }
        override fun onFailure(call: Call<List<User>>, t: Throwable) {
            continuation.resumeWithException(t)
        }
    })
}

现在您的函数是同步的,并返回一个Geo对象。