Hi, I am having converting this to kotlin from your java doc example. Can you translate to kotlin?. Got it working for the custom class approach but not the dict/map. Regards, Mike
No errors because I cannot figure out how to write the Kotlin code (not that familiar with Kotlin) . Android studio translates above java code to this…
val channel = Messaging.subscribe("demo")
channel.addMessageListener(object : AsyncCallback<HashMap<*, *>> {
override fun handleResponse(personObject: HashMap<*, *>) {
Log.i("MYAPP", "Received a message $personObject")
Log.i("MYAPP", "name - " + personObject["name"])
Log.i("MYAPP", "age - " + personObject["age"])
}
override fun handleFault(fault: BackendlessFault) {
Log.e("MYAPP", "Error processing a message $fault")
}
}, MessageObject::class.java)
but then complains with the line channel.addMessageListener(…)
None of the following functions can be called with the arguments supplied.
addMessageListener(AsyncCallback<TypeVariable(T)!>!, Class<TypeVariable(T)!>!)
where T = TypeVariable(T) for fun <T : Any!> addMessageListener(callback: AsyncCallback<T!>!, clazz: Class<T!>!): Unit defined in com.backendless.rt.messaging.Channel
addMessageListener(String!, AsyncCallback<String!>!) defined in comm.backendless.rt.messaging.Channel
addMessageListener(String!, MessageInfoCallback!) defined in com.backendless.rt.messaging.Channel
Soi basically I just don’t know how to convert the java supplied example to kotlin if that makes sense?
It seems that example itself is not completely correct. Channel.addMessageListener method receives as the second argument type of object with which AsyncCallback should work. In example provided by you, you want to handle instances of Person class while callback itself expects HashMap.
Java compiler wont compile such code. So I assume that the same error encountered Kotlin compiler.
Correct Java code with Person class will look in the next way:
final Channel channel = Backendless.Messaging.subscribe( “demo” );
channel.addMessageListener( new AsyncCallback()
{
@Override
public void handleResponse( Person personObject )
{
Log.i( “MYAPP”, "Received a message " + personObject );
Log.i( “MYAPP”, "name - " + personObject.getName() );
Log.i( “MYAPP”, "age - " + personObject.getAge() );
}
@Override
public void handleFault( BackendlessFault fault )
{
Log.e( “MYAPP”, "Error processing a message " + fault );
}
}, Person.class );
For this variant you also should provide definition for Person class.
Correct Java variant with HashMaps will be the next: