Felix Froschauer
Felix Froschauer

Reputation: 93

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

So today I was trying to use a post methode on an iis rest service in order to implement a login function in an Android-App. This rest service is protected by OAuth, so my main goal was to get the users access token.

But unfortunately my methode kept returning err 400, so i looked it up and found some solutions which didn't work in my case though.

Here's the methode:

 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];
}

and the error msg i am getting:

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

I would appreciate any help, cheers!

Upvotes: 1

Views: 205

Answers (1)

Johannes Gnadlinger
Johannes Gnadlinger

Reputation: 1365

Your Error code probably means that your username and password combination isnt right. Try to implement them hardcoded like I did:

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

Heres an github example from me for a login with owin and volley all you need is to insert your rest url in the LoginActivity: click me

Upvotes: 1

Related Questions