Android Volley gives me 400 error

后端 未结 12 2128
借酒劲吻你
借酒劲吻你 2020-12-11 00:28

I\'m trying to make a POST request to my API and it works in Postman (I get a valid JSON object), but not using Volley. With the follo

相关标签:
12条回答
  • 2020-12-11 01:12

    I have removed this params.put("Content-Type", "application/x-www-form-urlencoded");

    This is my Code Change.

    @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                if(!isLogout) {
                    params.put("username", username);
                    params.put("password", password);
                    params.put("grant_type", "password");
                } else {
                }
                return params;
            }
    
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                if(isLogout) {
                    params.put("Authorization", "bearer "+LibraryDataModel.getToken());
                }else {
                // Removed this line
                // params.put("Content-Type", "application/x-www-form-urlencoded");
                }
                return params;
            }
    
    0 讨论(0)
  • 2020-12-11 01:13

    In Android 2.3 there is a problem using Base64.encodeToString() as it introduces a new line in HTTP header. See my response to this question here in SO.

    Short answer: Don't use Base64.encodeToString() but put the already encoded String there.

    0 讨论(0)
  • 2020-12-11 01:14

    400 indicates a bad request, maybe you're missing Content-Type=application/json on your headers

    0 讨论(0)
  • 2020-12-11 01:14

    Somertimes this error 400 is arise due to Request type so you need to change the Request.Method.GET to Request.Method.POST and than it works like a charm.

    0 讨论(0)
  • 2020-12-11 01:16

    I have find a solution if you are using postman for hitting the api and your api have defined serializer field like skill = serializers.JSONField() then that type of error occurred-

    Solution For POSTMAN- you just add binary=True inside JSONField ex- skill = serializers.JSONField(binary=True)

    Solution for Android or other client then- you just remove binary=True inside JSONField ex- skill = serializers.JSONField()

    0 讨论(0)
  • 2020-12-11 01:18

    I've got this error and now Iit's been fixed. I did what is told in this link. What you need to do is

    1. go to src/com/android/volley/toolbox/BasicNetwork.java
    2. Change the following lines
     if (statusCode < 200 || statusCode > 299) {
         throw new IOException();
     }
    

    with

     if (statusCode < 200 || statusCode > 405) {
         throw new IOException();
     }
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题