1

I want to send an HTTP post about volley. According to IDE, I cannot overwrite the methods getBodyContentType and getBody, whether some examples on the net show this. What is the syntax (in braces) for it? What am I doing wrong?

error message: Modifier 'override' is not applicable to 'local function'

val textView = findViewById<TextView>(R.id.loginStatus)

val queue = Volley.newRequestQueue(this)
val url = "http://example.com/v1/user/read"


val stringRequest = StringRequest( Request.Method.GET, url,
    Response.Listener<String> { response ->
        textView.text = "Response is here"
    },
    Response.ErrorListener { textView.text = "Error." }

){
    override fun getBodyContentType(): String {
        return "application/json"
    }

    override fun getBody(): ByteArray {
        return "{\"email\":\"123@foo.de\", \"passwort\":\"123\"}".toString().toByteArray()
    }
}
Josef Soe
  • 91
  • 8

1 Answers1

5

The same can be achieved in Kotlin using object expression.

val stringRequest = object : StringRequest(...){ 

    override fun getBodyContentType(): String {
    ...
    }

    override fun getBody(): ByteArray {
    ...
    }

}
Akaki Kapanadze
  • 2,552
  • 2
  • 11
  • 21