【发布时间】:2019-05-09 00:50:02
【问题描述】:
我是 Kotlin 的新手,我有一个简单的问题: 我将我的所有项目从 java 转换为 kotlin,并成功地纠正了文档中的所有错误。
但我没有成功将 java 改造回调响应中的回调转换为 kotlin。
fun getUserAccountController(token: String, callback: UtilsCallback): Users {
userAccountServices.getUserAccount(token).enqueue(object : Callback<Result> {
override fun onResponse(call: Call<Result>, response: Response<Result>) {
if (response.isSuccessful) {
Log.i("Callback TOKEN ok ==> ", response.body()!!.resultUser!!.name)
user.name = response.body()!!.resultUser!!.name
user.username = response.body()!!.resultUser!!.username
callback.onData(user) // ==> Here my CALLBACK function
} else {
user.username = ""
user.name = ""
callback.onData(user) // ==> Here my CALLBACK function
}
}
override fun onFailure(call: Call<Result>, t: Throwable) {
Log.i("Callback TOKEN Nok ==> ", "error")
}
})
return user
}
我的回调是函数callback.onData
Handler().postDelayed({
user = userAccountController.getUserAccountController(token) {
fullName!!.text = user.name
firstName!!.text = user.name
email!!.text = user.username
}
}, 50)
}
它通过上面的这个转换我的代码,我不明白为什么。
下面是错误信息:
也许,我必须使用另一种方式?我在该示例中的目的是从成功的异步响应中获取我的数据,以便更新我的活动。
感谢阅读。
编辑
我还发布了我以前运行良好的 JAVA 代码
new Handler().postDelayed(new Runnable(){
@Override
public void run(){
user = userAccountController.getUserAccountController(token, new UtilsCallback() {
@Override
public void onData(Users userBack) {
fullName.setText(user.getName());
firstName.setText(user.getName());
email.setText(user.getUsername());
}
});
}
},50);
}
编辑 2 @Roland 建议的我的新 kotlin 代码
Handler().postDelayed({
user = userAccountController.getUserAccountController(token, UtilsCallback {
fullName!!.text = user.name
firstName!!.text = user.name
email!!.text = user.username
})
}, 50)
}
编辑 3:我的 kotlin 界面
package com.util.my3ciapplication
import com.model.my3ciapplication.Users
interface UtilsCallback {
fun onData(user: Users)
}
【问题讨论】:
-
欢迎来到 SO!您的问题应包括所有相关详细信息,请将您的错误以文本(而不是图像)的形式发布。