title: Make POST request to Api in Android Studio
body: <p>I'm trying to make a POST request to an api that I have created in Visual Studio. The api works, and I finally managed to find some code that allows me to connect to it (and it's not deprecated). The problem is that this code was made for a GET request while I need to make a POST. I created two boxes where I insert the data I want to pass (utente, password) and I created a button that takes the data from the boxex and convert them to string. </p>
  
  <p>I tried already searching a lot of examples and tutorials that show how to make a POST request but the majority are very old and doesn't work anymore in Android Studio, or at least I can't make them work.</p>
  
  <p>Now, this is the function that should be sending the data, I haven't touched the code since I don't really know what to modify except for the Request Method. </p>
  
  <pre class="lang-java prettyprint-override"><code>private StringRequest searchNameStringRequest(String utente, String password)
      {
          String url = "http://192.168.1.11:57279/api/utente";
          return new StringRequest(Request.Method.POST, url,
                  new Response.Listener&lt;String&gt;()
                  {
                      @Override
                      public void onResponse(String response)
                      {
                          try
                          {
                              JSONObject result = new JSONObject(response).getJSONObject("list");
                              int maxItems = result.getInt("end");
                              JSONArray resultList = result.getJSONArray("item");
                          }
                          catch (JSONException e)
                          {
                              Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                          }
                      }
                  },
                  new Response.ErrorListener()
                  {
                      @Override
                      public void onErrorResponse(VolleyError error)
                      {
                          Toast.makeText(MainActivity.this, "Food source is not responding (USDA API)", Toast.LENGTH_LONG).show();
                      }
                  });
      }
  </code></pre>
  
  <p>Can someone explain me how to take the data and send it like a JSON Object that has </p>
  
  <p>keys = user, password </p>
  
  <p>values = utente, password (the values are from the two boxes mentioned before)</p>
  
  <p>Thank to anyone who is willing to help me and I hope that asking for so much help isn't against the site rules.</p>
  
  <p>I'm using Volley since is not so complicated and because it seems to work.</p>
  
  <p>Using the GET method it show me the existing json with message cannot be converted to JSON object (I don't care about that, it's just a confirmation that it connects to the api)</p>
  
  <p>Using the POST method it throws the ErrorResponse at the end (Food source is not responding)</p>
  
  <p><strong>EDIT: Added OnCreate method since I need a StringRequest return</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          queue = Volley.newRequestQueue(this);
          Button invia = findViewById(R.id.submit);
          final EditText utenteInserito = findViewById(R.id.utente);
          final EditText passwordInserito = findViewById(R.id.password);
  
          invia.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  String utente = utenteInserito.getText().toString();
                  String password = passwordInserito.getText().toString();
                  queue.cancelAll(R.id.submit);
                  StringRequest stringRequest = searchNameStringRequest(utente, password);
                  stringRequest.setTag(R.id.submit);
                  queue.add(stringRequest);
              }
          });
      }
  </code></pre>
  
  <p><strong>EDIT: I have followed the suggested answer given but it doesn't seem to work</strong></p>
  
  <p>The resulting code is shown below but I get the OnErrorResponse, I don't think it's a problem with the api because trying with a GET response it gives me the exiting json array, so I think it's a problem with the code.</p>
  
  <pre><code>private StringRequest searchNameStringRequest(final String utente, final String password)
      {
          String url = "http://192.168.1.11:57279/api/utente";
          StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener&lt;String&gt;()
          {
              @Override
              public void onResponse(String response)
              {
                  System.out.println(response);
              }
          }, new Response.ErrorListener()
          {
  
              @Override
              public void onErrorResponse(VolleyError error)
              {
                  Toast.makeText(MainActivity.this,"Service Unavailable",Toast.LENGTH_SHORT).show();
                  error.printStackTrace();
              }
          })
          {
              @Override
              protected Map&lt;String, String&gt; getParams()
              {
                  Map&lt;String,String&gt; map = new HashMap&lt;&gt;();
                  map.put("user", utente.trim());
                  map.put("password",password.trim());
                  return map;
              }
          };
          RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
          requestQueue.add(request);
          return request;
      }
  </code></pre>