Kotlin 如何从方法返回值获取数据
我使用科特林 我想从方法中获取数据Kotlin 如何从方法返回值获取数据,kotlin,Kotlin,我使用科特林 我想从方法中获取数据 fun Example() : String{ var value : String client.newCall(request).enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { print("Getting Data Failed") }
fun Example() : String{
var value : String
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
print("Getting Data Failed")
}
override fun onResponse(call: Call, response: Response) {
value = "aaa"
}
}
return value
}
最后我想得到价值。。
但是回调函数工作得很晚
如何获取价值数据(“aaa”)据我从您的问题中了解,您希望同步执行请求。这可以通过以下代码完成:
fun Example(): String {
val response = client.newCall(request).execute()
val value = if (response.isSuccessful()) {
"aaa"
} else {
error("request failed")
}
return value
}