Volley not functioning in kotlin like it was working in java

I am trying to integrated a volley request of JSONObjectRequest in Kotlin

 var requestToRegisterUser = JsonObjectRequest(Request.Method.POST,BASE_URL +        USER_REGISTRATION , bodyParams,
            Response.Listener { response ->
                toast(response.toString())

            },
            Response.ErrorListener { error ->
                toast(error.toString())
            })
{
override fun getHeaders() : Map<String,String> {
  }
}

And it says
modifier override is not applicable to local function

On the other end I am able to use it StringRequest

val sessionRequest = object : StringRequest(Request.Method.GET, BASE_URL, Response.Listener { s ->
        doCallForWhoAmI()
    }, Response.ErrorListener { e ->

    }) {
        override fun parseNetworkResponse(response: NetworkResponse?): Response<String> {
            Log.i("response", response.toString())
            val cookiesInfo : java.util.TreeMap<String,String> = response?.headers as TreeMap<String, String>
            val cookie = cookiesInfo.get("Set-Cookie")

            var prefHandler = PreferenceHandler(this@SplashActivity)
            prefHandler.saveSession(cookie as String)

            return super.parseNetworkResponse(response)
        }
    }

Like these but instead of method parseNetworkResponse I want to use getHeaders Method to attach some values at post request.

you should declare your first val same as the second one with object : … .
in the first declaration you simply create a new instance of JsonObjectRequest whereas in the second delcaration you define an instance of a class which inherits from StringRequest which in turn can override any open method in its parent class.

1 Like

Can you please show me by some code if you have done it with.

I am trying that block but it seems it is not looking like it.

var requestToRegisterUser = object : JsonObjectRequest(Request.Method.POST,BASE_URL +        USER_REGISTRATION , bodyParams,
            Response.Listener { response ->
                toast(response.toString())

            },
            Response.ErrorListener { error ->
                toast(error.toString())
            })
{
override fun getHeaders() : Map<String,String> {
  }
}

fun login(){
email = etemail.text.toString().trim()
pass = etpass.text.toString().trim()
if (email != “” && pass != “”){
val stringRequest = StringRequest(Request.Method.POST,url, { response →
if (response == “Success”){
val intent = Intent(this,MainActivity::class.java)
startActivity(intent)
finish()
}
else if(response == “failure”){
Toast.makeText(this,“Invalide login Email/Password”,Toast.LENGTH_SHORT).show()
}

        },
            { error ->
                Toast.makeText(this,error.toString().trim(),Toast.LENGTH_SHORT).show()
            }
        ) {
            @Throws(AuthFailureError::class)
            fun main(): Map<String, String> {
                val map = HashMap<String, String>()
                map["email"] = email
                map["password"] = pass
                return map
            }
        }
        val requestQueue : RequestQueue = Volley.newRequestQueue(applicationContext)
        requestQueue.add(stringRequest)

    }else{
        Toast.makeText(this,"Fields can not be empty!", Toast.LENGTH_SHORT).show()
    }

can you tell me what wron in this because it shows fun main is never used