如何在Android中使用Volley发送帖子请求?

时间:2017-09-02 03:16:58

标签: java android android-volley

我正在发送像这样的截击请求

  btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            //submitForm();
            JsonObjectRequest jsonobjectRequest = new JsonObjectRequest(Request.Method.POST, URL, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    //errorlabel.setText(response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    errorlabel.setText("Invalid username / password");
                }

            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("email", "asd@asd.com");
                    params.put("password", "asd");



                    return params;
                }
            };
            errorlabel.setText(jsonobjectRequest.toString());
            requestQueue.add(jsonobjectRequest);

        }
    });




}

但是我从服务器收到一条错误消息,指出无效的电子邮件/密码。

我已经设置了正确的参数。我在Postman上测试了它,它在那里工作。这是一个截图。

截图

enter image description here

1 个答案:

答案 0 :(得分:1)

我有同样的问题,我尝试使用String请求,其工作

 StringRequest jsonObjRequest = new StringRequest(Request.Method.POST,
               URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {

            @Override
            public String getBodyContentType() {
                return "application/x-www-form-urlencoded; charset=UTF-8";
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {


                Map<String, String> postParam = new HashMap<String, String>();

                postParam.put("email", "asd@asd.com");
                postParam.put("password", "asd");


                return postParam;
            }

        };

        requestQueue.add(jsonObjRequest);