title: Using this code to fetch JSON data from an API, I want to post JSON data to an API in a similar fashion
body: <p>Tried multiple solutions but no success.</p>
  <p>I need to post using HttpURLConnection</p>
  <p>code.java</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      public class DownloadTask extends AsyncTask&lt;String,Void,String&gt; {
  
          @Override
          protected String doInBackground(String... urls) {
              String result = &quot;&quot;;
              URL url;
              HttpURLConnection urlConnection = null;
  
              try {
  
                  url = new URL(urls[0]);
                  urlConnection = (HttpURLConnection) url.openConnection();
                  InputStream in = urlConnection.getInputStream();
                  InputStreamReader reader = new InputStreamReader(in);
                  int data = reader.read();
  
                  while (data != -1) {
                      char current = (char) data;
                      result += current;
                      data = reader.read();
                  }
  
                  return result;
  
              } catch (Exception e) {
                  e.printStackTrace();
                  return null;
              }
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
  
              try {
                  JSONObject jsonObject = new JSONObject(s);
  
                  String weatherInfo = jsonObject.getString(&quot;weather&quot;);
  
                  Log.i(&quot;Weather content&quot;, weatherInfo);
  
                  JSONArray arr = new JSONArray(weatherInfo);
  
                  for (int i=0; i &lt; arr.length(); i++) {
                      JSONObject jsonPart = arr.getJSONObject(i);
  
                      Log.i(&quot;main&quot;,jsonPart.getString(&quot;main&quot;));
                      Log.i(&quot;description&quot;,jsonPart.getString(&quot;description&quot;));
                  }
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
  
          }
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          DownloadTask task = new DownloadTask();
          task.execute(&quot;http://samples.openweathermap.org/data/2.5/weather?q=London,uk&amp;appid=b1b15e88fa797225412429c1c50c122a1&quot;);
      }
  }
  </code></pre>
  <p>post.java</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          DownloadTask task = new DownloadTask();
          task.execute(&quot;https://reqres.in/api/users&quot;);
      }
      public class DownloadTask extends AsyncTask&lt;String,Void,String&gt; {
          HttpURLConnection httpURLConnection = null;
          String result = &quot;&quot;;
          InputStream inputStream;
          OutputStream outputStream;
  
          protected String doInBackground(String... params) {
              try {
                  URL url = new URL(params[0]);
                  httpURLConnection = (HttpURLConnection) url.openConnection();
                  httpURLConnection.setDoInput(true);
                  httpURLConnection.setDoOutput(true);
                  httpURLConnection.setRequestMethod(&quot;POST&quot;);
                  httpURLConnection.setRequestProperty(&quot;Accept&quot;, &quot;*/*&quot;);
  
                  ContentValues values = new ContentValues();
                  values.put(&quot;email&quot;, &quot;abc@tjk.com&quot;);
                  values.put(&quot;password&quot;, &quot;hjh&quot;);
                  values.put(&quot;name&quot;, &quot;hui&quot;);
  
                  httpURLConnection.connect();
  
                  outputStream = httpURLConnection.getOutputStream();
  
                  BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
                  bufferedWriter.write(getQuery(values));
                  bufferedWriter.flush();
  
                  int statusCode = httpURLConnection.getResponseCode();
  
                  inputStream = httpURLConnection.getInputStream();
  
                  if (statusCode == 200) {
                      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                      int data = inputStreamReader.read();
                      while (data != -1) {
                          char current = (char) data;
                          result += current;
                          data = inputStreamReader.read();
                      }
  
                      JSONObject jsonObject = new JSONObject(result);
  
                      Log.i(&quot;Result&quot;, String.valueOf(jsonObject));
  
                  } else {
                      Log.i(&quot;Result&quot;, &quot;false&quot;);
                  }
  
              } catch (Exception e) {
  
                  e.printStackTrace();
              } finally {
                  try {
                      if (inputStream != null) {
                          inputStream.close();
                      }
                      if (outputStream != null) {
                          outputStream.close();
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
              return null;
          }
  
          private String getQuery(ContentValues values) throws UnsupportedEncodingException {
              StringBuilder result = new StringBuilder();
              boolean first = true;
  
              for (Map.Entry&lt;String, Object&gt; entry : values.valueSet()) {
                  if (first)
                      first = false;
                  else
                      result.append(&quot;&amp;&quot;);
  
                  result.append(URLEncoder.encode(entry.getKey(), &quot;UTF-8&quot;));
                  result.append(&quot;=&quot;);
                  result.append(URLEncoder.encode(String.valueOf(entry.getValue()), &quot;UTF-8&quot;));
              }
  
              Log.i(&quot;Result&quot;, result.toString());
  
              return result.toString();
          }
  
      }
  }
  </code></pre>