【问题标题】:Can not override getParam in Kotlin (Volley)无法覆盖 Kotlin(Volley)中的 getParam
【发布时间】:2020-11-25 13:29:57
【问题描述】:

我无法覆盖getParam,如下所示。我希望有人可以解释如何在 Kotlin 中覆盖 getParam

build.gradle

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

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)
}

【问题讨论】:

    标签: android kotlin post android-volley


    【解决方案1】:

    您不能,它不会在 StringRequest 或任何其他内置请求中公开。如果这确实是您需要做的,那么不幸的是您必须create your own custom request

    以下是自定义StringRequest 的示例,它允许我们在其构造函数中指定params (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)
            )
        }
    }
    

    然后你会像这样使用它:

    // 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)
    

    【讨论】:

    • 请查看更新后的答案,添加一个示例并稍微修正一下代码。
    • 哇,感谢您的快速答复。我测试了你的代码,效果很好!我也搜索了一下,找到了一个我会分享但 100% 不明白的选项。
    【解决方案2】:

    在 StringRequest 之前键入对象,现在方法 getParams 可用。

    代码如下所示:

    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)
        }
    

    【讨论】:

    • 这也有效,因为您没有将它装箱到StringRequest 类,而是装箱到暴露getParams 的底层Response 类。 +1 为简单起见
    • 没问题对不起 * Request class not Response.
    • 这是一篇不错的帖子,其中包含指向更多 stackoverflow.com/a/44604198/1133011 的链接,以更好地解释“匿名内部类”
    • 将您的回答标记为解决方案,它更简单,可能是要走的路!
    • 好的,但感谢您的时间和解释! (可以在 2 天后接受自己的答案)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    相关资源
    最近更新 更多