Android Kotlin - Volley set POST Parameters in JsonArrayRequest

好久不见. 提交于 2021-01-29 20:21:34

问题


This is the code:

val requestQueue: RequestQueue = Volley.newRequestQueue(this@MainActivity)

val jsonArrayRequest = JsonArrayRequest(
    Request.Method.POST,
    "$domain/do_getmemes.php",
    null,
    Response.Listener { response ->

    },
    Response.ErrorListener { // Do something when error occurred

    }
)

requestQueue.add(jsonArrayRequest)

and I just want to add some Parameters!

I've seen this JAVA example: https://gist.github.com/mstfldmr/f6594b2337e3633673e5

but I don't know what/where/how to add the parameters from this abomination of example.

I tried to add this part right after JsonArrayRequest():

  {
    @Override
    protected Map<String,String> getParams(){
      Map<String,String> params = new HashMap<String, String>();
      params.put("user","YOUR USERNAME");
      params.put("pass","YOUR PASSWORD");
      return params;
    }
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
      Map<String,String> params = new HashMap<String, String>();
      params.put("Content-Type","application/x-www-form-urlencoded");
      return params;
    }
  }

but it doesn't get converted to Kotlin.

I need to send some Ints and Strings


回答1:


add object so you can override functions

val jsonArrayRequest = object : JsonArrayRequest(
    Request.Method.POST,
    "$domain/do_getmemes.php",
    null,
    Response.Listener { response ->

    },
    Response.ErrorListener { // Do something when error occurred

    }
) {
    override fun getBody(): ByteArray {
        val parameters = HashMap<String, String>()
        parameters["key"] = "value"
        return JSONObject(parameters.toString()).toString().toByteArray()
    }
}


来源:https://stackoverflow.com/questions/62461515/android-kotlin-volley-set-post-parameters-in-jsonarrayrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!