title: Volley request with headers and body params
body: <p>I need to make an api request which. </p>
  
  <p>Two headers : </p>
  
  <ol>
  <li><p>Accept</p></li>
  <li><p>Authorization</p></li>
  </ol>
  
  <p>Five body params.</p>
  
  <ol>
  <li>Number</li>
  <li>Make</li>
  <li>Model</li>
  <li>Description</li>
  <li>Plates</li>
  </ol>
  
  <p>Through postman everything works great. 
  But when i try through android app i can't get through. 
  Note: Login through the same host works great so the setup its not the problem i think my main problem is in api call.</p>
  
  <pre><code>public void add(View view) {
          RequestQueue queue = Volley.newRequestQueue(this);
          String URL = "http://10.0.2.2:8000/api/trucks";
          StringRequest request = new StringRequest(Request.Method.POST, URL,
                  new Response.Listener&lt;String&gt;() {
                      @Override
                      public void onResponse(String response) {
                          try {
                              JSONObject jsonObj = new JSONObject(response);
                              // parse response
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
                  },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          NetworkResponse response = error.networkResponse;
                          String errorMsg = "";
                          if (response != null &amp;&amp; response.data != null) {
                              String errorString = new String(response.data);
                          }
                      }
                  }
          ) {
  
              @Override
              public Map&lt;String, String&gt; getHeaders() {
                  HashMap&lt;String, String&gt; headers = new HashMap&lt;&gt;();
                  headers.put("Accept", "application/json");
                  headers.put("Authorization", "Bearer " + myToken);
                  return headers;
              }
  
              @Override
              protected Map&lt;String, String&gt; getParams() {
                  Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
                  TextInputEditText number = findViewById(R.id.textInputEditTextNumber);
                  TextInputEditText make = findViewById(R.id.textInputEditTextMake);
                  TextInputEditText model = findViewById(R.id.textInputEditTextModel);
                  TextInputEditText description = findViewById(R.id.textInputEditTextDescription);
                  TextInputEditText plates = findViewById(R.id.textInputEditTextPlates);
  
                  params.put("number", number.getText().toString());
                  params.put("make", make.getText().toString());
                  params.put("model", model.getText().toString());
                  params.put("description", description.getText().toString());
                  params.put("plates", plates.getText().toString());
                  return params;
              }
          };
          queue.add(request);
      }
  </code></pre>
  
  <p>Edit: by Solution #1.</p>
  
  <pre><code>public void add(View view) throws JSONException {
      RequestQueue queue = Volley.newRequestQueue(this);
      TextInputEditText number = findViewById(R.id.textInputEditTextNumber);
      TextInputEditText make = findViewById(R.id.textInputEditTextMake);
      TextInputEditText model = findViewById(R.id.textInputEditTextModel);
      TextInputEditText description = findViewById(R.id.textInputEditTextDescription);
      TextInputEditText plates = findViewById(R.id.textInputEditTextPlates);
  
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("Number", number.getText().toString());
      jsonObject.put("Make", make.getText().toString());
      jsonObject.put("Model", model.getText().toString());
      jsonObject.put("Description", description.getText().toString());
      jsonObject.put("Plates", plates.getText().toString());
      final String requestBody = jsonObject.toString();
  
      JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, "http://10.0.2.2:8000/api/trucks", jsonObject,
              new Response.Listener&lt;JSONObject&gt;() {
          @Override
          public void onResponse(JSONObject response) {
  
              //now handle the response
              Toast.makeText(truck_add.this, response.toString(), Toast.LENGTH_SHORT).show();
  
          }
      }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
  
              //handle the error
              Toast.makeText(truck_add.this, "An error occurred", Toast.LENGTH_SHORT).show();
              error.printStackTrace();
          }
      }) {    //this is the part, that adds the header to the request
          @Override
          public Map&lt;String, String&gt; getHeaders() {
              Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
              params.put("Accept", "application/json");
              params.put("Authorization", myToken);
              return params;
          }
      };
      queue.add(jsonRequest);
  }
  </code></pre>
  
  <p>Postman : <a href="https://i.stack.imgur.com/NSxM9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NSxM9.png" alt="Postman example"></a></p>