I am a newcomer to Kotlin and I have a simple question : I convert all my project from java to kotlin and I succeed to correct all bugs with the documentation.
But I did not succeed to convert a call back inside java retrofit callback response to 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
}
My callback is the function callback.onData
Handler().postDelayed({
user = userAccountController.getUserAccountController(token) {
fullName!!.text = user.name
firstName!!.text = user.name
email!!.text = user.username
}
}, 50)
}
And it converts my code by this above I do not understand why.
Here is below the message of the error :
Maybe, I have to do use another way? My purpose in that exemple is to get my data from the succeed asynchrone response in the order to update my activity.
Thanks for reading.
EDIT
I post also my previous JAVA code which worked well
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);
}
EDIT 2 My new kotlin code suggested by @Roland
Handler().postDelayed({
user = userAccountController.getUserAccountController(token, UtilsCallback {
fullName!!.text = user.name
firstName!!.text = user.name
email!!.text = user.username
})
}, 50)
}
EDIT 3 : My kotlin interface
package com.util.my3ciapplication
import com.model.my3ciapplication.Users
interface UtilsCallback {
fun onData(user: Users)
}