我致力于开发一个 android 应用程序,我想制作一个通用的 volley post 请求功能,我将我的功能编写如下:
public fun <T> push(context: Context, url: String, myObject: T, completion: (response: String) -> Unit) {
val queue = Volley.newRequestQueue(context)
val sr = object : StringRequest(
Method.POST, url,
Response.Listener { response ->
println(response)
completion(response)
},
Response.ErrorListener { volleyError ->
Common.showVolleyError(volleyError, context)
}) {
override fun getParams(): Map<String, String> {
val params = myObject as HashMap<String, String>
return params
}
@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val params = HashMap<String, String>()
params["Content-Type"] = "application/x-www-form-urlencoded"
params["X-Requested-With"] = "XMLHttpRequest"
return params
}
}
sr.retryPolicy = DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
queue.add(sr)
}
我强制执行的是如何将我的可序列化对象转换为一个HashMap<String, String>(),即如何绑定myObject到getParams()函数,
不负相思意
暮色呼如
相关分类