0

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)
}
ShikaZero
  • 23
  • 7
  • Welcome to SO! Your question should include all relevant details, please post your error as text (not as image). – cheersmate Dec 07 '18 at 09:20

2 Answers2

1

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

Roland
  • 22,259
  • 4
  • 57
  • 84
  • I tried but it doest not work :s thanks for answering – ShikaZero Dec 07 '18 at 09:32
  • what was the problem when you did? – Roland Dec 07 '18 at 09:38
  • UtilsCallBack is an inteface so it told me that n interface cannot have constructors. – ShikaZero Dec 07 '18 at 10:09
  • `UtilsCallback {` <-- this is not a constructor call... you are sure you used it the same way? – Roland Dec 07 '18 at 10:42
  • I post my previsous JAVA code on EDIT in my main post. – ShikaZero Dec 07 '18 at 10:55
  • look at my code, carefully... it should work... I just saw that there was a bracket too much, but the main part stays the same... `UtilsCallback {`, not `UtilsCallback() {`... and you need to put it in the `getUserAccountController(`-brackets... if it still doesn't work, please show your code then... – Roland Dec 07 '18 at 11:21
  • 1
    Hello thanks for you help first of all. I post the code you suggest me with the following screen error. – ShikaZero Dec 07 '18 at 12:21
  • ah... this is a Kotlin interface? please have a look at: [Kotlin interface ... does not have constructors](https://stackoverflow.com/questions/43737785/kotlin-interface-does-not-have-constructors)... you could basically exchange that `UtilsCallback` with a nice `() -> Unit` if you want... (or what is the most appropriate for you)... that way you can then even omit `UtilsCallback` and place it outside your brackets, as before... – Roland Dec 07 '18 at 12:34
0

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)
}
ShikaZero
  • 23
  • 7
  • while you can do that, I would rather suggest you to use the Kotlin functional style (e.g. `(Users) -> Unit`) instead... even more so, if that interface is just a Kotlin interface you got while transforming your Java code ;-) – Roland Dec 10 '18 at 15:27