Android 无法覆盖Kotlin中的getParam(截击)
我无法覆盖如下所示的Android 无法覆盖Kotlin中的getParam(截击),android,kotlin,post,android-volley,Android,Kotlin,Post,Android Volley,我无法覆盖如下所示的getParam。我希望有人能解释如何在Kotlin中重写getParam 格雷德尔先生 implementation 'com.android.volley:volley:1.1.1' 您不能,它不会在StringRequest或任何其他内置请求中公开。如果这真的是你需要做的,不幸的是你不得不这么做 下面是一个自定义StringRequest的示例,它允许我们在其构造函数(Kotlin)中指定params: 在StringRequest之前键入object,现在getP
getParam
。我希望有人能解释如何在Kotlin中重写getParam
格雷德尔先生
implementation 'com.android.volley:volley:1.1.1'
您不能,它不会在
StringRequest
或任何其他内置请求中公开。如果这真的是你需要做的,不幸的是你不得不这么做
下面是一个自定义StringRequest
的示例,它允许我们在其构造函数(Kotlin)中指定params
:
在StringRequest之前键入object,现在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)
}
fun testpost(按钮:按钮)
{
val url=”http://192.168.178.23/insertcode.php"
val queue=Volley.newRequestQueue(此)
val stringRequest=object:stringRequest(Request.Method.POST,url,
{响应->
button.text=“响应为:${Response}”
},
{button.text=“那没用!”)
{
//按Ctr+O查找getParams
重写fun getParams():MutableMap{
val hashMap=hashMap()
hashMap.put(“name”,“peter”)
返回哈希映射
}
}
添加(stringRequest)
}
请查看更新的答案,添加了一个示例,并对代码进行了一点修改。哇,谢谢您的快速回答。我测试了你的代码,效果非常好!我也搜索了一点,找到了一个我将分享但不完全理解的选项。这也很有效,因为您没有将它放入StringRequest
类,而是放入底层Response
类,该类公开getParams
+1为简单起见没问题抱歉*请求
类不响应
。这里有一个很好的帖子,链接到更多更好地解释“匿名内部类”标记你的答案作为解决方案,它更简单,可能是一种方式!好的,我会的,但谢谢你的时间和解释!(2天后可以接受自己的答案)
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)
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)
}