无论我通过什么参数,截击响应都相同

时间:2019-07-08 14:59:17

标签: android android-volley token wordpress-rest-api

我正在将POST请求发送到wp-rest-api以获取令牌,以便从android应用登录用户,无论我在此处传递的用户名和密码是我的请求代码,还是得到相同的响应

private void login() {
    RequestQueue queue = Volley.newRequestQueue(this);

    StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //
            Toast.makeText(MainActivity.this, "bad", Toast.LENGTH_LONG).show();

        }
    }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();

            params.put("username","isohunter1@hotmail.com");
            params.put("password","qweqweqwe123");
            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;
        }
    };
    queue.add(sr);
}

1 个答案:

答案 0 :(得分:0)

您的代码看起来正确。它与我的仅相差几个参数。可能值得一试:

        @Override
        protected Map<String, String> getParams() 
        {  
                Map<String, String>  params = new HashMap<String, String>();  
                params.put("grant_type", "password");
                params.put("username","isohunter1@hotmail.com");
                params.put("password","qweqweqwe123");

                return params;  
        }

       @Override
       public Map<String, String> getHeaders() throws AuthFailureError {
           HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Accept", "application/x-www-form-urlencoded; charset=UTF-8");
                headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
           return headers;
       }

您在onResponse中得到什么响应?