【问题标题】:the volley response is the same regardless of parameters i pass无论我通过什么参数,凌空响应都是相同的
【发布时间】:2019-07-08 14:59:17
【问题描述】:

我正在向 wp-rest-api 发送 POST 请求以获取令牌,以便从 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);
}

【问题讨论】:

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


    【解决方案1】:

    您的代码看起来正确。它与我的只有几个参数不同。可能值得一试:

            @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 中得到什么响应?

    【讨论】:

    • 我对每个用户名和密码、令牌然后是用户名(测试用户名)、显示名(测试显示名)都得到相同的响应,我的意思是当我更改用户名和密码时,响应包含旧的用户名和密码
    • 嗯,这与您调用 API 的方式无关,这是 API 处理请求方式的某些因素,它不在您的掌控之中。在我的代码中,我忽略了除了用于其他连续 HTTP 请求的令牌之外的任何其他返回值。
    猜你喜欢
    • 1970-01-01
    • 2018-07-09
    • 2023-04-02
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2020-08-23
    相关资源
    最近更新 更多