Can not override getParam in Kotlin (Volley)

Viewed 611

I can not override getParam like shown below. I hope someone can explain how to override getParam in Kotlin.

build.gradle

implementation 'com.android.volley:volley:1.1.1'

enter image description here

fun testpost(button: Button)
{
    val url = "http://192.168.178.23/insertcode.php"
    val queue = Volley.newRequestQueue(this)


    val stringRequest = StringRequest(Request.Method.POST, url,
            { response ->
                button.text = "Response is: ${response}"
            },
            { button.text = "That didn't work!" })
    {
        override fun getParam(){

        }
    }
    queue.add(stringRequest)
}
2 Answers

Typing object before StringRequest and now the method getParams is available.

The code looks like this:

fun testpost(button: Button)
    {
        val url = "http://192.168.178.23/insertcode.php"
        val queue = Volley.newRequestQueue(this)


        val stringRequest = object :StringRequest(Request.Method.POST, url,
            { response ->
                button.text = "Response is: ${response}"
            },
            { button.text = "That didn't work!" })
        {
            //Press Ctr + O to find getParams
            override fun getParams(): MutableMap<String, String> {
                val hashMap = HashMap<String, String>()
                hashMap.put("name", "peter")
                return hashMap
            }
        }
        queue.add(stringRequest)
    }

You can't, it's not exposed in StringRequest or any other built in request for that matter. If that's indeed what you need to do, you have to unfortunately create your own custom request.

Below is an example for a Custom StringRequest which allows us to specify params in its constructor (Kotlin):

import androidx.annotation.GuardedBy
import com.android.volley.NetworkResponse
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.HttpHeaderParser
import java.io.UnsupportedEncodingException
import java.nio.charset.Charset

class CustomStringRequest(
    method: Int,
    url: String,
    listener: Response.Listener<String>,
    errorListener: Response.ErrorListener?,
    private val params: MutableMap<String, String>
) : Request<String>(method, url, errorListener) {

    private val lock = Any()

    @GuardedBy("lock")
    private var listener: Response.Listener<String>? = listener

    override fun getParams(): MutableMap<String, String> {
        return params
    }

    override fun cancel() {
        super.cancel()
        synchronized(lock) { listener = null }
    }

    override fun deliverResponse(response: String) {
        var listener: Response.Listener<String>?
        synchronized(lock) { listener = this.listener }
        if (listener != null) {
            listener!!.onResponse(response)
        }
    }

    override fun parseNetworkResponse(response: NetworkResponse): Response<String> {
        val parsed: String = try {
            String(response.data, Charset.forName(HttpHeaderParser.parseCharset(response?.headers)))
        } catch (e: UnsupportedEncodingException) {
            // Since minSdkVersion = 8, we can't call
            // new String(response.data, Charset.defaultCharset())
            // So suppress the warning instead.
            String(response.data)
        }

        return Response.success(
            parsed,
            HttpHeaderParser.parseCacheHeaders(response)
        )
    }
}

You would then use it like:

// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(activity)
val url = "YOUR_URL"

// Request a string response from the provided URL.
val stringRequest = CustomStringRequest(
    Request.Method.POST, url,
    Response.Listener { response ->
        // TODO do something with response
    },
    Response.ErrorListener {
        // TODO handle errors
    },
    hashMapOf("name" to "value") // TODO add your params here
)

// Add the request to the RequestQueue.
queue.add(stringRequest)
Related