尝试使用 volley 的休息服务,但它一直给我响应代码 400

trying to consume a rest service with volley but it keeps giving me the response code 400

所以今天我试图在 iis rest 服务上使用 post 方法,以便在 Android-App 中实现登录功能。此休息服务受 OAuth 保护,因此我的主要目标是获取用户访问令牌。

但不幸的是,我的方法一直返回错误 400,所以我查找并找到了一些对我的情况不起作用的解决方案。

方法如下:

 public boolean validateUser(final String username, final String password)
 {
    String url="not allowed to show the url here";
    final boolean[] validUser = {false};
    StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject obj = new JSONObject(response);
                        //String token=obj.getString("token");
                    } catch (JSONException e) {
                        System.out.println("couldn't parse string to jsonObject");
                    }

                    Log.d("RESPONSE",response);
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("volley", "Error: " + error.getMessage());
            error.printStackTrace();
        }
    }) {

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded"; //tried a couple of things like "application/json" and "application/x-www-form-urlencoded, encoding UTF-8"
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("username", username.trim());
            params.put("password", password.trim());
            params.put("grant_type", "password");
            return params;
        }

       /* @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            return headers;
        }*/
    };
    RequestQueue requestQueue= Volley.newRequestQueue(this);

    requestQueue.add(jsonObjRequest);
    requestQueue.start();

    //AppController.getInstance().addToRequestQueue(jsonObjRequest);
    return validUser[0];
}

以及我收到的错误消息:

E/Volley: [176] BasicNetwork.performRequest: Unexpected response code 400 for "not allowed to show the url"

我将不胜感激,干杯!

您的错误代码可能意味着您的用户名和密码组合不正确。尝试像我一样硬编码实现它们:

String url="foo";
    final RequestQueue queue = Volley.newRequestQueue(this);


    StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    // response
                    Log.d("Response", response);
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error

                    Log.d("Error.Response", "ERROR");
                }
            }
    ) {
        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded";
        }
        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("username", "diplomarbeittest");
            params.put("password", "iscool");
            params.put("grant_type", "password");

            return params;
        }
    };
    queue.add(postRequest);

这是我用 owin 和 volley 登录的一个 github 示例,您只需在 LoginActivity 中插入其余部分 url: click me