Android Volley code 400

别来无恙 提交于 2019-12-25 06:43:55

问题


I'm trying to post data to website. It is login operation. I check form data hidden tag etc. In form data there is a hidden input _RequestVerificationToken. That's why first, I made get request to parse _RequestVerificationToken and for headers.

StringRequest _StringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                       //Parsing Process
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }
           )
            {
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {

                headers=response.headers;
                return super.parseNetworkResponse(response);
            }
        };

After all i made post request with parameters and headers:

@Override
            public Map<String, String> getParams()
            {
                Map<String, String>  params = new HashMap<>();
                // the POST parameters:
                params.put("Password", password);
                params.put("RememberMe", "false");
                params.put("ReturnUrl", url_post);
                params.put("UserName",username);
                params.put(name,_RequestVerificationToken);
                return params;
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                              return headers;
            }

I check form data by using fiddler extension in chrome. I checked parameters again and again but it returned code 400. Can you show me what is the problem?


回答1:


Change Request.Method.GET to Request.Method.POST

  StringRequest _StringRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                           //Parsing Process
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                        }
                    }
               )
                {
                @Override
                protected Response<String> parseNetworkResponse(NetworkResponse response) {

                    headers=response.headers;
                    return super.parseNetworkResponse(response);
                }
            };

Or You can use this code:-

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

                                returnValue = response;
                                try {
                                    parseFromUrl(response);
                                    System.out.println(returnValue);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                 Toast.makeText(app,error.toString(),Toast.LENGTH_SHORT).show();

                            }
                        }) {
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("Password", password);
                        params.put("RememberMe", "false");
                        params.put("ReturnUrl", url_post);
                        params.put("UserName",username);
                        params.put(name,_RequestVerificationToken);
                        return params;
                    }

                };

                RequestQueue requestQueue = Volley.newRequestQueue(app);
                requestQueue.add(stringRequest);



回答2:


I solve the problem with this class and I added this:

 CookieManager cookieManager = new CookieManager(new PersistentCookieStore(this), CookiePolicy.ACCEPT_ORIGINAL_SERVER);
            CookieHandler.setDefault(cookieManager);

before add request to queue



来源:https://stackoverflow.com/questions/35893461/android-volley-code-400

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