title: Android HttpsURLConnection - simple POST request with parameters
body: <p>I've been trying to get a simple android client server app working, and I've had nothing but trouble.  I'm hoping someone can look at this code and see what I'm doing wrong, maybe I'm doing things in the wrong order, or forgetting something?  Just adding the relevant parts</p>
  
  <pre><code>            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
              conn.addRequestProperty("Content-Type", "application/json");
  
              // set some made up parameters
              String str =  "{'login':'superman@super.com','password':'password'}";
              byte[] outputInBytes = str.getBytes("UTF-8");
              OutputStream os = conn.getOutputStream();
              os.write( outputInBytes );
              os.close();
  
              // connection.setDoOutput(true); //should trigger POST - move above -&gt; crash
              conn.setRequestMethod("POST");  // explicitly set POST -move above so we can set params -&gt; crash
              conn.setDoInput(true);
  </code></pre>
  
  <p>The error I get is </p>
  
  <blockquote>
    <p>'exception: java.net.ProtocolException: method does not support a
    request body: GET'</p>
  </blockquote>
  
  <p>If I just do a POST request without parameters it's fine, so I guess I should move the connection.setDoOutput(true); or conn.setRequestMethod("POST"); higher up, that should work right?  When I do that I get the error:</p>
  
  <blockquote>
    <p>exception: java.net.ProtocolException: Connection already established.</p>
  </blockquote>
  
  <p>So, if I try to set to POST before adding parameters it doesn't work, if I try to do it after it doesn't work... what am I missing?  Is there another way I should be doing this?  Am I adding parameters incorrectly?  I've been searching for a simple android networking example, and I can't find any, is there any example the official Android site?  All I want to do is a very basic network operation, this is so frustrating!</p>
  
  <p>EDIT: I need to use HttpsURLConnection for reasons not included in the above code- I need to authenticate, trust hosts, etc- so I'm really looking for a potential fix for the above code if possible.</p>