Android Volley gives me 400 error

后端 未结 12 2145
借酒劲吻你
借酒劲吻你 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条回答
  •  猫巷女王i
    2020-12-11 00:57

    If you are passing the json value to in body(raw json).No need to set the content type as headers.put("Content-Type", "application/json; charset=utf-8");

    When I was using the content type in header callback ,the 400 response status I was getting in logcat.I commented headers.put("Content-Type", "application/json; charset=utf-8"); as because I am passing raw json in body while calling api.screenshot for postman is attached.It will help

    private void volleyCall(String email, String password) {
    
          RequestQueue queue= Volley.newRequestQueue(this);
          String URL = "http://XXXX.in:8080/XXXX/api/userService/login";
    
          Map jsonParams = new HashMap();
          jsonParams.put("email", email);
          jsonParams.put("password", password);
          Log.d(TAG,"Json:"+ new JSONObject(jsonParams));
    
          JsonObjectRequest postRequest = new JsonObjectRequest( Request.Method.POST, URL,new JSONObject(jsonParams),
                  new Response.Listener() {
                      @Override
                      public void onResponse(JSONObject response) {
                          Log.d(TAG,"Json"+ response);
                      }
                  },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          //   Handle Error
                          Log.d(TAG, "Error: " + error
                                  + "\nStatus Code " + error.networkResponse.statusCode
                                  + "\nResponse Data " + error.networkResponse.data
                                  + "\nCause " + error.getCause()
                                  + "\nmessage" + error.getMessage());
                      }
                  }) {
              @Override
              public Map getHeaders() throws AuthFailureError {
                  HashMap headers = new HashMap();
                 // headers.put("Content-Type", "application/json; charset=utf-8");
                  return headers;
              }
              @Override
              public String getBodyContentType() {
                  return "application/json";
              }
    
    
    
          };
    
          queue.add(postRequest);
    
      }
    

    LogCat:
    LoginActivity: Json:{"email":"XXXXXX@gmail.com","password":"abcde"}
    LoginActivity: Error: com.android.volley.ServerError
                                                                              
    Status Code 400                                                                     
    Response Data [B@63ca992                                                                              
    Cause null                                                                              
    messagenull
    

提交回复
热议问题