ShikaZero
ShikaZero

Reputation: 23

How convert a callback inside retrofit callback response from java to Kotlin

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 :

Kotlin code showing 'type mismatch' 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)
}

Kotlin code showing 'interface does not have constructors' error

EDIT 3 : My kotlin interface

    package com.util.my3ciapplication

import com.model.my3ciapplication.Users

interface UtilsCallback {

    fun onData(user: Users)
}

Upvotes: 0

Views: 1057

Answers (2)

ShikaZero
ShikaZero

Reputation: 23

Thanks Roland for you help !

Here is the final correct response for helping.

val obj = object : UtilsCallback{
        override fun onData(user: Users) {
            fullName!!.text = user.name
            firstName!!.text = user.name
            email!!.text = user.username
        }
    }

    Handler().postDelayed({
        user = userAccountController.getUserAccountController(token, obj)
    }, 50)
}

Upvotes: 0

Roland
Roland

Reputation: 23272

Actually it says it there in the picture... you use () -> Unit and you should use UtilsCallback instead...

Probably the following will already work:

Handler().postDelayed({
  user = userAccountController.getUserAccountController(token, UtilsCallback {
        fullName!!.text = user.name
        firstName!!.text = user.name
        email!!.text = user.username
    })
  }, 50)

Or at least the solution will probably be very similar to this.

Update: I was assuming that was a Java interface... For your second error, please have also a look at Kotlin: Interface ... does not have constructors.

In that specific case I would rather replace your UtilsCallback with a similarly appropriate functional counterpart, e.g. () -> Unit

Upvotes: 1

Related Questions