android volley gives client error code 400












0















I'm trying to make a POST request to get token from my web API (writen by asp.net mvc) and it works in Postman (I get a valid JSON object), but not using Volley. With the following code:



  String url = "https://branj.ir/Token";

Response.Listener<String> listener= new Response.Listener<String>() {
@Override
public void onResponse(String jsonResult) {

Log.i(G.Tag, "volley result: " + jsonResult);
}
};
Response.ErrorListener errorListener= new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

error.printStackTrace();
Log.d(G.Tag, "Error: " + error
+ "nStatus Code " + error.networkResponse.statusCode
+ "nCause " + error.getCause()
+ "nmessage" + error.getMessage());
}
};

StringRequest request = new StringRequest (
Request.Method.POST,
url,
listener,
errorListener

)
{
@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");
params.put("Accept", "application/json");
return params;
} }
;


logcat is:



 BasicNetwork.performRequest: Unexpected response code 400 for https://branj.ir/Token
11-13 16:55:59.608 21200-21200/ir.branj.app.wideidea.ir.branj D/Branj: Error: com.android.volley.ClientError
Status Code 400
Cause null
messagenull









share|improve this question























  • Error 400 is returned by the server because request wasn't correct. If you want to get response that was sent by the server in case of error then you have to read error.networkResponse.data in onErrorResponse function. error.getMessage() isn't the right place to look for the server side response.

    – Sharj
    Nov 13 '18 at 19:09











  • check this STRINGREQUEST code :- stackoverflow.com/a/52531318/7319704

    – Abhinav Gupta
    Nov 14 '18 at 5:43
















0















I'm trying to make a POST request to get token from my web API (writen by asp.net mvc) and it works in Postman (I get a valid JSON object), but not using Volley. With the following code:



  String url = "https://branj.ir/Token";

Response.Listener<String> listener= new Response.Listener<String>() {
@Override
public void onResponse(String jsonResult) {

Log.i(G.Tag, "volley result: " + jsonResult);
}
};
Response.ErrorListener errorListener= new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

error.printStackTrace();
Log.d(G.Tag, "Error: " + error
+ "nStatus Code " + error.networkResponse.statusCode
+ "nCause " + error.getCause()
+ "nmessage" + error.getMessage());
}
};

StringRequest request = new StringRequest (
Request.Method.POST,
url,
listener,
errorListener

)
{
@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");
params.put("Accept", "application/json");
return params;
} }
;


logcat is:



 BasicNetwork.performRequest: Unexpected response code 400 for https://branj.ir/Token
11-13 16:55:59.608 21200-21200/ir.branj.app.wideidea.ir.branj D/Branj: Error: com.android.volley.ClientError
Status Code 400
Cause null
messagenull









share|improve this question























  • Error 400 is returned by the server because request wasn't correct. If you want to get response that was sent by the server in case of error then you have to read error.networkResponse.data in onErrorResponse function. error.getMessage() isn't the right place to look for the server side response.

    – Sharj
    Nov 13 '18 at 19:09











  • check this STRINGREQUEST code :- stackoverflow.com/a/52531318/7319704

    – Abhinav Gupta
    Nov 14 '18 at 5:43














0












0








0








I'm trying to make a POST request to get token from my web API (writen by asp.net mvc) and it works in Postman (I get a valid JSON object), but not using Volley. With the following code:



  String url = "https://branj.ir/Token";

Response.Listener<String> listener= new Response.Listener<String>() {
@Override
public void onResponse(String jsonResult) {

Log.i(G.Tag, "volley result: " + jsonResult);
}
};
Response.ErrorListener errorListener= new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

error.printStackTrace();
Log.d(G.Tag, "Error: " + error
+ "nStatus Code " + error.networkResponse.statusCode
+ "nCause " + error.getCause()
+ "nmessage" + error.getMessage());
}
};

StringRequest request = new StringRequest (
Request.Method.POST,
url,
listener,
errorListener

)
{
@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");
params.put("Accept", "application/json");
return params;
} }
;


logcat is:



 BasicNetwork.performRequest: Unexpected response code 400 for https://branj.ir/Token
11-13 16:55:59.608 21200-21200/ir.branj.app.wideidea.ir.branj D/Branj: Error: com.android.volley.ClientError
Status Code 400
Cause null
messagenull









share|improve this question














I'm trying to make a POST request to get token from my web API (writen by asp.net mvc) and it works in Postman (I get a valid JSON object), but not using Volley. With the following code:



  String url = "https://branj.ir/Token";

Response.Listener<String> listener= new Response.Listener<String>() {
@Override
public void onResponse(String jsonResult) {

Log.i(G.Tag, "volley result: " + jsonResult);
}
};
Response.ErrorListener errorListener= new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

error.printStackTrace();
Log.d(G.Tag, "Error: " + error
+ "nStatus Code " + error.networkResponse.statusCode
+ "nCause " + error.getCause()
+ "nmessage" + error.getMessage());
}
};

StringRequest request = new StringRequest (
Request.Method.POST,
url,
listener,
errorListener

)
{
@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");
params.put("Accept", "application/json");
return params;
} }
;


logcat is:



 BasicNetwork.performRequest: Unexpected response code 400 for https://branj.ir/Token
11-13 16:55:59.608 21200-21200/ir.branj.app.wideidea.ir.branj D/Branj: Error: com.android.volley.ClientError
Status Code 400
Cause null
messagenull






android api model-view-controller android-volley






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 14:30









Mahdi SarAiiMahdi SarAii

1415




1415













  • Error 400 is returned by the server because request wasn't correct. If you want to get response that was sent by the server in case of error then you have to read error.networkResponse.data in onErrorResponse function. error.getMessage() isn't the right place to look for the server side response.

    – Sharj
    Nov 13 '18 at 19:09











  • check this STRINGREQUEST code :- stackoverflow.com/a/52531318/7319704

    – Abhinav Gupta
    Nov 14 '18 at 5:43



















  • Error 400 is returned by the server because request wasn't correct. If you want to get response that was sent by the server in case of error then you have to read error.networkResponse.data in onErrorResponse function. error.getMessage() isn't the right place to look for the server side response.

    – Sharj
    Nov 13 '18 at 19:09











  • check this STRINGREQUEST code :- stackoverflow.com/a/52531318/7319704

    – Abhinav Gupta
    Nov 14 '18 at 5:43

















Error 400 is returned by the server because request wasn't correct. If you want to get response that was sent by the server in case of error then you have to read error.networkResponse.data in onErrorResponse function. error.getMessage() isn't the right place to look for the server side response.

– Sharj
Nov 13 '18 at 19:09





Error 400 is returned by the server because request wasn't correct. If you want to get response that was sent by the server in case of error then you have to read error.networkResponse.data in onErrorResponse function. error.getMessage() isn't the right place to look for the server side response.

– Sharj
Nov 13 '18 at 19:09













check this STRINGREQUEST code :- stackoverflow.com/a/52531318/7319704

– Abhinav Gupta
Nov 14 '18 at 5:43





check this STRINGREQUEST code :- stackoverflow.com/a/52531318/7319704

– Abhinav Gupta
Nov 14 '18 at 5:43












2 Answers
2






active

oldest

votes


















0














{
"error": "unsupported_grant_type"
}
when i have used your request on postman i am getting the error because the body is incorrect of the volley.






share|improve this answer
























  • use this parameters as body Map<String,String> params = new HashMap<>(); params.put("username", "NamadTest"); params.put("password", "NamadTest"); params.put("grant_type", "password");

    – Mahdi SarAii
    Nov 14 '18 at 4:47





















0














I changed the application code as follows and my problem was resolved



 public static void Token(final String username, final String password) {

String url = BASE_URL + "Token";

Response.Listener<String> listener= new Response.Listener<String>() {
@Override
public void onResponse(String jsonResult) {

Log.i(G.Tag, "volley result: " + jsonResult);
}
};
Response.ErrorListener errorListener= new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d(G.Tag, "Error: " + error
+ "nStatus Code " + error.networkResponse.statusCode
+ "nCause " + error.getCause()
+ "nnetworkResponse " + error.networkResponse.data.toString()
+ "nmessage" + error.getMessage());
}
};

StringRequest request = new StringRequest (
Request.Method.POST,
url,
listener,
errorListener
)
{
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("username", username);
params.put("password", password);
params.put("grant_type", "password");
return params;
}
} ;
G.getInstance().addToRequestQueue(request);
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283264%2fandroid-volley-gives-client-error-code-400%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    {
    "error": "unsupported_grant_type"
    }
    when i have used your request on postman i am getting the error because the body is incorrect of the volley.






    share|improve this answer
























    • use this parameters as body Map<String,String> params = new HashMap<>(); params.put("username", "NamadTest"); params.put("password", "NamadTest"); params.put("grant_type", "password");

      – Mahdi SarAii
      Nov 14 '18 at 4:47


















    0














    {
    "error": "unsupported_grant_type"
    }
    when i have used your request on postman i am getting the error because the body is incorrect of the volley.






    share|improve this answer
























    • use this parameters as body Map<String,String> params = new HashMap<>(); params.put("username", "NamadTest"); params.put("password", "NamadTest"); params.put("grant_type", "password");

      – Mahdi SarAii
      Nov 14 '18 at 4:47
















    0












    0








    0







    {
    "error": "unsupported_grant_type"
    }
    when i have used your request on postman i am getting the error because the body is incorrect of the volley.






    share|improve this answer













    {
    "error": "unsupported_grant_type"
    }
    when i have used your request on postman i am getting the error because the body is incorrect of the volley.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 19:02









    Vishal SharmaVishal Sharma

    7762212




    7762212













    • use this parameters as body Map<String,String> params = new HashMap<>(); params.put("username", "NamadTest"); params.put("password", "NamadTest"); params.put("grant_type", "password");

      – Mahdi SarAii
      Nov 14 '18 at 4:47





















    • use this parameters as body Map<String,String> params = new HashMap<>(); params.put("username", "NamadTest"); params.put("password", "NamadTest"); params.put("grant_type", "password");

      – Mahdi SarAii
      Nov 14 '18 at 4:47



















    use this parameters as body Map<String,String> params = new HashMap<>(); params.put("username", "NamadTest"); params.put("password", "NamadTest"); params.put("grant_type", "password");

    – Mahdi SarAii
    Nov 14 '18 at 4:47







    use this parameters as body Map<String,String> params = new HashMap<>(); params.put("username", "NamadTest"); params.put("password", "NamadTest"); params.put("grant_type", "password");

    – Mahdi SarAii
    Nov 14 '18 at 4:47















    0














    I changed the application code as follows and my problem was resolved



     public static void Token(final String username, final String password) {

    String url = BASE_URL + "Token";

    Response.Listener<String> listener= new Response.Listener<String>() {
    @Override
    public void onResponse(String jsonResult) {

    Log.i(G.Tag, "volley result: " + jsonResult);
    }
    };
    Response.ErrorListener errorListener= new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    error.printStackTrace();
    Log.d(G.Tag, "Error: " + error
    + "nStatus Code " + error.networkResponse.statusCode
    + "nCause " + error.getCause()
    + "nnetworkResponse " + error.networkResponse.data.toString()
    + "nmessage" + error.getMessage());
    }
    };

    StringRequest request = new StringRequest (
    Request.Method.POST,
    url,
    listener,
    errorListener
    )
    {
    @Override
    public Map<String, String> getParams() throws AuthFailureError {
    Map<String,String> params = new HashMap<>();
    params.put("username", username);
    params.put("password", password);
    params.put("grant_type", "password");
    return params;
    }
    } ;
    G.getInstance().addToRequestQueue(request);
    }





    share|improve this answer






























      0














      I changed the application code as follows and my problem was resolved



       public static void Token(final String username, final String password) {

      String url = BASE_URL + "Token";

      Response.Listener<String> listener= new Response.Listener<String>() {
      @Override
      public void onResponse(String jsonResult) {

      Log.i(G.Tag, "volley result: " + jsonResult);
      }
      };
      Response.ErrorListener errorListener= new Response.ErrorListener() {
      @Override
      public void onErrorResponse(VolleyError error) {
      error.printStackTrace();
      Log.d(G.Tag, "Error: " + error
      + "nStatus Code " + error.networkResponse.statusCode
      + "nCause " + error.getCause()
      + "nnetworkResponse " + error.networkResponse.data.toString()
      + "nmessage" + error.getMessage());
      }
      };

      StringRequest request = new StringRequest (
      Request.Method.POST,
      url,
      listener,
      errorListener
      )
      {
      @Override
      public Map<String, String> getParams() throws AuthFailureError {
      Map<String,String> params = new HashMap<>();
      params.put("username", username);
      params.put("password", password);
      params.put("grant_type", "password");
      return params;
      }
      } ;
      G.getInstance().addToRequestQueue(request);
      }





      share|improve this answer




























        0












        0








        0







        I changed the application code as follows and my problem was resolved



         public static void Token(final String username, final String password) {

        String url = BASE_URL + "Token";

        Response.Listener<String> listener= new Response.Listener<String>() {
        @Override
        public void onResponse(String jsonResult) {

        Log.i(G.Tag, "volley result: " + jsonResult);
        }
        };
        Response.ErrorListener errorListener= new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
        Log.d(G.Tag, "Error: " + error
        + "nStatus Code " + error.networkResponse.statusCode
        + "nCause " + error.getCause()
        + "nnetworkResponse " + error.networkResponse.data.toString()
        + "nmessage" + error.getMessage());
        }
        };

        StringRequest request = new StringRequest (
        Request.Method.POST,
        url,
        listener,
        errorListener
        )
        {
        @Override
        public Map<String, String> getParams() throws AuthFailureError {
        Map<String,String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("grant_type", "password");
        return params;
        }
        } ;
        G.getInstance().addToRequestQueue(request);
        }





        share|improve this answer















        I changed the application code as follows and my problem was resolved



         public static void Token(final String username, final String password) {

        String url = BASE_URL + "Token";

        Response.Listener<String> listener= new Response.Listener<String>() {
        @Override
        public void onResponse(String jsonResult) {

        Log.i(G.Tag, "volley result: " + jsonResult);
        }
        };
        Response.ErrorListener errorListener= new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
        Log.d(G.Tag, "Error: " + error
        + "nStatus Code " + error.networkResponse.statusCode
        + "nCause " + error.getCause()
        + "nnetworkResponse " + error.networkResponse.data.toString()
        + "nmessage" + error.getMessage());
        }
        };

        StringRequest request = new StringRequest (
        Request.Method.POST,
        url,
        listener,
        errorListener
        )
        {
        @Override
        public Map<String, String> getParams() throws AuthFailureError {
        Map<String,String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("grant_type", "password");
        return params;
        }
        } ;
        G.getInstance().addToRequestQueue(request);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 17 '18 at 10:34

























        answered Nov 14 '18 at 5:34









        Mahdi SarAiiMahdi SarAii

        1415




        1415






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283264%2fandroid-volley-gives-client-error-code-400%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine