Volley StringRequest发送参数POST方法

时间:2019-11-22 06:41:34

标签: android kotlin https android-volley

class test2 : AppCompatActivity(){

 override fun onCreate(savedInstanceState: Bundle?)
 {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_test2)
    connect()

 }

 fun connect(){
    val queue = Volley.newRequestQueue(this)
    val url = "https://www.example.com/Register.php"
    val params = HashMap<String,String>()
    params["abc"] = "parm1"
    params["def"] = "parm2"
    params["ghi"] = "parm3"

    val stringRequest = StringRequest(Request.Method.POST, url,
        Response.Listener<String> { response ->
            text_test.text = "Response is: $response"
        },
        Response.ErrorListener { text_test.text = "That didn't work! " })
    queue.add(stringRequest)
  }
}

参数应为"param1=data1&param2=data2&param3=data3"格式

我可以使用表格数据在邮递员中发送数据

有什么主意我可以从我的代码发送数据吗?

请认真发送帮助,这是我第二次询问

这是邮递员https://ibb.co/SQgG72W的图片

1 个答案:

答案 0 :(得分:1)

您必须重写getParams方法。这样您就可以将要发送的参数传递给服务器

StringRequest sr = new StringRequest(Request.Method.GET, "http://headers.jsontest.com/",
  new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
      Log.e("HttpClient", "success! response: " + response.toString());
    }
  },
  new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
      Log.e("HttpClient", "error: " + error.toString());
    }
  })
  {
    @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;
    }
  };