【问题标题】:trying to consume a rest service with volley but it keeps giving me the response code 400尝试使用 volley 使用休息服务,但它一直给我响应代码 400
【发布时间】:2017-07-29 08:56:37
【问题描述】:

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

但不幸的是,我的方法一直返回 err 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:“不允许显示 url”的意外响应代码 400

我会很感激任何帮助,干杯!

【问题讨论】:

  • stackoverflow.com/questions/19671317/…。 “400表示请求格式错误。也就是说,客户端发送给服务器的数据流不符合规则。” - 检查您的网址,查看密码/用户名是否正确
  • 您的 Rest 请求是否适用于邮递员等其他客户端?
  • 是的,它适用于邮递员,我什至使用相同的编码/参数
  • 你能从后端显示代码吗?
  • 是朋友做的,如果好的话我明天可以发在这里

标签: android rest oauth android-volley http-status-code-400


【解决方案1】:

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

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);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 2014-07-03
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多