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

Asked

Viewed 27 times

0

I have this error 'Type Mismatch' 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 solve?

  • Why not reuse the variable declared above that already has the answer? editTextRespostaAviario?.text = body

  • It doesn’t work that way @Leonardolima

1 answer

1


When you use properties access syntax (using .), you must pass a Editable to the 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);
  • Thank you, that way no more mistakes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.