Error: mismatch "$ {body.toString ()}" in Kotlin

0

I have this 'Type mismatch' error in this code '$ {body.toString ()}':

    fun fetchJson(){

    val url = "http://localhost:8080/matematica3/naoAutomatica/get"

    val request = Request.Builder().url(url).build()

    val client = OkHttpClient()
    client.newCall(request).enqueue(object : Callback {
        override fun onFailure(call: Call?, e: IOException?) {
            println("${e?.message}")
        }

        override fun onResponse(call: Call?, response: Response?) {
            val body = response?.body()?.string()
            println(body)

            editTextRespostaAviario?.text = "${body.toString()}"
        }

    })
}

How can I resolve?

    
asked by anonymous 05.07.2018 / 17:30

1 answer

1

When you use property access syntax (using . ), you must pass Editable to EditText . To pass a String , use the setter:

editTextRespostaAviario?.setText("${body.toString()}"

Note that the above syntax would be redundant, since you are using a String Template to pass only one String. The ideal is to pass the String directly:

editTextRespostaAviario?.setText(body.toString());

It is also redundant to call toString in the body, since it itself is already a String . The following should be enough for you:

editTextRespostaAviario?.setText(body);
    
05.07.2018 / 19:13