我正在尝试使用Retrofit call.enqueue(DrawCircles())来获取数据,当我调试值时,但是我认为它不会等待,并且我的函数会继续执行其余的代码行。因此,当我运行它时,结果列表(myListCoord)始终为空,如何进行同步调用。
这是我的代码:
doAsync {
var a = DrawCircles()
myListCoord = a.runCircles()
}
fun runCircles(): List<Coordinates>? {
val request = ServiceBuilder.buildService(TmdbEndpoints::class.java)
val call = request.getCorrdinates()
call.enqueue(object : Callback<MyList> {
override fun onResponse(call: Call<MyList>, response: Response<MyList>) {
if (response.isSuccessful){
Toast.makeText(this@DrawCircles, "Succès", Toast.LENGTH_LONG).show()
myListCoord = response.body()!!.locations
}
}
override fun onFailure(call: Call<MyList>, t: Throwable) {
Toast.makeText(this@DrawCircles, "${t.message}", Toast.LENGTH_SHORT).show()
}
})
return myListCoord
}
您是否尝试过使用call.execute()
而不是call.enqueue()
?
从docs:
void enqueue(Callback<T> callback)
异步地发送请求,并将响应通知给回调函数,或者在与服务器通信,创建请求或处理响应时发生错误。
Response<T> execute() throws IOException
同步发送请求并返回其响应。
((重点))>