title: Android Getting = java.net.ProtocolException: exceeded content-length limit of 0 bytes, when writing to OutpuStream
body: <p>I'm trying to make a post call with HttpURLConnection &amp; sending JSON data via HttpURLConnection's OutputStream: </p>
  
  <p>See my class below:</p>
  
  <pre><code>package com.app.data.pojos;
  
  import android.os.AsyncTask;
  import android.util.Log;
  import com.app.xyz.data.GDParser;
  import com.app.xyz.model.GDUser;
  import com.app.xyz.model.GDUserStatus;
  import com.app.xyz.utils.APrefs;
  import com.google.gson.JsonElement;
  import com.google.gson.JsonObject;
  import com.google.gson.JsonParser;
  import org.json.JSONObject;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  /**
   * Created by arynaim on 10/12/17.
   */
  
  public class PostUserStatus extends AsyncTask&lt;String, Void, GDUserStatusResponse&gt; {
  
      private static final String TAG = "PostUserStatus";
      private GDUser gdUser;
  
      public PostUserStatus(GDUser gdUser) {
          this.gdUser = gdUser;
      }
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
  
      }
  
      @Override
      protected GDUserStatusResponse doInBackground(String... urls) {
  
          Log.i(TAG,"doInBackground(), gDUserStatusResponse url:"+urls[0]);
          HttpURLConnection httpURLConnection = null;
          GDUserStatus gdUserStatus = null;
          JsonElement jsonElemnt = null;
          JsonObject jsonObject = null;
          GDUserStatusResponse gDUserStatusResponse = null;
          try {
              URL url = new URL(urls[0]);
              httpURLConnection = (HttpURLConnection) url.openConnection();
              httpURLConnection.setRequestMethod("POST");
              httpURLConnection.setRequestProperty("Content-length", "0");
              httpURLConnection.setUseCaches(false);
              httpURLConnection.setAllowUserInteraction(false);
              httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
              httpURLConnection.setRequestProperty("Authorization", new APrefs().getNMToken());
              //httpURLConnection.connect();
              JSONObject userIdJsonObject = new JSONObject();
              userIdJsonObject.put("userID",getGdUser().getUserId());
              //post json via OutputStream
              OutputStream os = httpURLConnection.getOutputStream();
              os.write(userIdJsonObject.toString().getBytes());
              os.flush();
              Log.i(TAG,"doInBackground(), http request"+url.toString());
  
  
              int status = httpURLConnection.getResponseCode();
  
              switch (status) {
                  case 200:
                      Log.i(TAG,"doInBackground(), http response status:"+status);
                      BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                      StringBuilder sb = new StringBuilder();
                      String line;
                      while ((line = br.readLine()) != null) {
                          sb.append(line + "\n");
                      }
  
                      jsonElemnt = new JsonParser().parse(sb.toString());
                      jsonObject = jsonElemnt.getAsJsonObject();
                      gdUserStatus = new GDParser().parseStatusResponse(jsonObject);
                      gDUserStatusResponse = new GDUserStatusResponse();
                      gDUserStatusResponse.setGdUserStatus(gdUserStatus);
                      br.close();
                      break;
                  default:
                      Log.e(TAG,"doInBackground(), http response error status:"+status);
                      //TODO: handle error responses
                      //TODO: if stale token refresh &amp; try again
                      break;
  
              }
  
  
  
          } catch (IOException ex) {
  
              ex.printStackTrace();
  
          } catch (Exception ex) {
  
              ex.printStackTrace();
  
          } finally {
              if (httpURLConnection != null) {
                  try {
                      httpURLConnection.disconnect();
                  } catch (Exception ex) {
                  ex.printStackTrace();
                  }
              }
          }
  
          return gDUserStatusResponse;
      }//end doInBackgroud
  
      protected void onPostExecute(GDUserStatusResponse gDUserStatusResponse) {
  
          Log.i(TAG,"onPostExecute(), gDUserStatusResponse:"+gDUserStatusResponse);
  
      }
  
      public GDUser getGdUser() {
          return gdUser;
      }
  
      public void setGdUser(GDUser gdUser) {
          this.gdUser = gdUser;
      }
  }
  </code></pre>
  
  <p>However when I write to  </p>
  
  <pre><code>os.write(userIdJsonObject.toString().getBytes());
  </code></pre>
  
  <p>I'm getting this exception:</p>
  
  <pre><code>10-13 16:30:46.737 15395-17467/com.app.xyz I/PostUserStatus: doInBackground(), gDUserStatusResponse url:https://development.xyz.com/api/v1/users/status
  10-13 16:30:46.741 15395-17467/com.app.xyz I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
  10-13 16:30:46.741 15395-17467/com.app.xyz I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err: java.net.ProtocolException: exceeded content-length limit of 0 bytes
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err:     at com.android.okhttp.internal.http.RetryableSink.write(RetryableSink.java:58)
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err:     at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err:     at com.android.okhttp.okio.RealBufferedSink$1.flush(RealBufferedSink.java:204)
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err:     at com.app.xyz.data.pojos.PostUserStatus.doInBackground(PostUserStatus.java:63)
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err:     at com.app.xyz.data.pojos.PostUserStatus.doInBackground(PostUserStatus.java:24)
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:304)
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
  10-13 16:30:46.833 15395-17467/com.app.xyz W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
  10-13 16:30:46.834 15395-17467/com.app.xyz W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
  10-13 16:30:46.834 15395-17467/com.app.xyz W/System.err:     at java.lang.Thread.run(Thread.java:762)
  10-13 16:30:46.835 15395-15395/com.app.xyz I/PostUserStatus: onPostExecute(), gDUserStatusResponse:null
  </code></pre>
  
  <p>I don't want to use any Http libraries other than the base provided with Android/Java.</p>