在 Volley 中发出发布请求的通用函数

我致力于开发一个 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()函数,


拉风的咖菲猫
浏览 111回答 2
2回答

不负相思意

最后,对于任何可能喜欢使用这种方式的人,我将函数重写如下:public fun <T> push(context: Context, url: String, myObject: T,myObjectType : Array<Field>, completion: (response: String) -> Unit) {&nbsp; &nbsp; val myObjectAsDict = HashMap<String, String>()&nbsp; &nbsp; val allFields = myObjectType //:Array<Field> = myObjectType!!::class.java.declaredFields&nbsp; &nbsp; for ( field in allFields) {&nbsp; &nbsp; &nbsp; &nbsp; if (!field.isAccessible) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.isAccessible = true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; val value = field.get(myObject)&nbsp; &nbsp; &nbsp; &nbsp; if (value != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( field.name != "serialVersionUID") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myObjectAsDict[field.name] = value.toString()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; println(myObjectAsDict)&nbsp; &nbsp; val queue = Volley.newRequestQueue(context)&nbsp; &nbsp; val sr = object : StringRequest(&nbsp; &nbsp; &nbsp; &nbsp; Method.POST, url,&nbsp; &nbsp; &nbsp; &nbsp; Response.Listener { response ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println(response)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; completion(response)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; Response.ErrorListener { volleyError ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Common.showVolleyError(volleyError, context)&nbsp; &nbsp; &nbsp; &nbsp; }) {&nbsp; &nbsp; &nbsp; &nbsp; override fun getParams(): Map<String, String> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val params = myObjectAsDict&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return params&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Throws(AuthFailureError::class)&nbsp; &nbsp; &nbsp; &nbsp; override fun getHeaders(): Map<String, String> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val params = HashMap<String, String>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params["Content-Type"] = "application/x-www-form-urlencoded"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params["X-Requested-With"] = "XMLHttpRequest"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return params&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; sr.retryPolicy = DefaultRetryPolicy(&nbsp; &nbsp; &nbsp; &nbsp; 0,&nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_MAX_RETRIES,&nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_BACKOFF_MULT&nbsp; &nbsp; )&nbsp; &nbsp; queue.add(sr)}并使用它如下:&nbsp;var myClass = MyClass()&nbsp; &nbsp; &nbsp; &nbsp; VolleyFunctions.push(this,"URL",myClass, MyClass::class.java.declaredFields)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myClass = Gson().fromJson(response, MyClass::class.java)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println("myClass.Name${myClass.name}")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;

暮色呼如

使基类包含一个抽象方法返回Map<String, String>命名为 example getConvertedParams。此方法应将自身转换为Map<String, String>:val params = HashMap<String, String>()params["attribute1"] = attribute1params["attribute2"] = attribute2...return params每个请求对象都应该扩展该基类并覆盖该方法。在getParams您发送请求的地方,调用getConvertedParams您的通用请求对象。override fun getParams(): Map<String, String> {&nbsp; &nbsp;val params = myObject.getConvertedParams()&nbsp; &nbsp;return params}也不要忘记更改方法签名public fun <BaseClassName> push(context: Context, url: String, myObject: BaseClassName, completion: (response: String) -> Unit)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java