无法覆盖 Kotlin 中的 getParam (Volley)
Can not override getParam in Kotlin (Volley)
我无法覆盖 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)
}
你不能,它没有在 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)
在 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)
}
我无法覆盖 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)
}
你不能,它没有在 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)
在 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)
}