简体   繁体   中英

Android login POST request using Volley

Hi guys I have a problem with Volley POST request. I want to log in into page where is just box to login user. Response from this request is just html code of this loging box. It should log me in(like POSTman code bellow) and then I can do other things that I need to do. Login data in code are not valid just example.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         requestQueue = Volley.newRequestQueue(this);
         requestQueue.start();
    }

    @Override
    public void onClick(View v) {
        if (v == buttonLogin) {
            loginUser();
        }
    }

    public void loginUser() {
         StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://ns3.apis.sk:28080/login_check",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    getJsonData();
                    Log.d("Response", response);
                    Toast.makeText(LoginActivity.this, response, Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<>();
                params.put("_username","admin");
                params.put("_password","root");
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                final HashMap<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "multipart/form-data");
                return headers;
        }
    };
    requestQueue.add(stringRequest);
}

Also there is POSTman code which is doing the thing I want to achieve in Android properlly.

     {
        "name": "http://ns3.apis.sk:28080 - login_check",
        "request": {
            "method": "POST",
            "header": [
                {
                    "key": "Content-Type",
                    "name": "Content-Type",
                    "value": "application/json",
                    "type": "text"
                }
            ],
            "body": {
                "mode": "formdata",
                "formdata": [
                    {
                        "key": "_username",
                        "value": "admin",
                        "type": "text"
                    },
                    {
                        "key": "_password",
                        "value": "root",
                        "type": "text"
                    }
                ]
            },
            "url": {
                "raw": "http://ns3.apis.sk:28080/login_check",
                "protocol": "http",
                "host": [
                    "ns3",
                    "apis",
                    "sk"
                ],
                "port": "28080",
                "path": [
                    "login_check"
                ]
            }
        },
        "response": []
    }

use this code for header

@Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            final HashMap<String, String> headers = new HashMap<>();
           // headers.put("Content-Type", "multipart/form-data");
            return headers;
 @Override
        public String getBodyContentType() {
            return "multipart/form-data";
        }

use this for field

JSONObject jsonParams = new JSONObject();
jsonParams .put("_username","admin");
jsonParams .put("_password","root");

i hope this will help you

Hi guys so finally solved it. Removed getheaders() method. Probably Volley library knows what to use implictly. And the most importand is enable cookies because Volley has it disabled by default.

@Override
    protected void onCreate(Bundle savedInstanceState) {
         CookieManager manager = new CookieManager();
         CookieHandler.setDefault(manager);
         requestQueue = Volley.newRequestQueue(this);
         requestQueue.start();
    }

    @Override
    public void onClick(View v) {
        if (v == buttonLogin) {
            loginUser();
        }
    }

    public void loginUser() {
         StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://ns3.apis.sk:28080/login_check",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    getJsonData();
                    Log.d("Response", response);
                    Toast.makeText(LoginActivity.this, response, Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<>();
                params.put("_username","admin");
                params.put("_password","root");
                return params;
            }
    };
    requestQueue.add(stringRequest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM