Android URLCOnnection post JSON is passing null to server
<p>I was using a HttpClient to post JSON data to a server like so:</p>

<pre><code>        httppost.setHeader("json", json.toString());
        httppost.getParams().setParameter("jsonpost", postjson);

        // Execute HTTP Post Request
        System.out.print(json);
        HttpResponse response = httpclient.execute(httppost);
</code></pre>

<p>and that worked fine - most of the time. However, I've read that to deal with the occasional timeouts on the server, I should move to a URLConnection type of communication.</p>

<p>I've recoded to the below, but it's not sending data.  My server says it's getting NULL while my Android JSON data I wish to send is in the form of {"user_id":123}.  What am I doing wrong that my server is not getting the data?</p>

<pre><code>  public String postPullData(JSONObject json, String URL) { 
    int CONNECTION_TIMEOUT = 15*1000;
    int WAIT_RESPONSE_TIMEOUT = 15*1000;
    String Error = "SCOMM Error: ";
    JSONArray postjson;
    URL url = null;



    // setup the connection URL to the server
    try {
        url = new URL(URL);
    } catch (MalformedURLException e) {
        Error += "MalformedURLException  " + e;
        Log.d("SCOMM", Error);
        e.printStackTrace();
        return Error;
    }


    StringBuilder sb = new StringBuilder();


    // open the connection
    try {

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("content-type","application/json; charset=utf-8"); 
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");  

        // setup the write
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

        // write it
        out.write(json.toString());
        out.flush();

        // close the write stream
        out.close();

        // setup the read stream
        BufferedReader in = new BufferedReader( new InputStreamReader( connection.getInputStream()));

        // read it
        String decodedString = null;
        while ((decodedString = in.readLine()) != null) {
            sb.append(decodedString + "\n");
        }

        // close the read stream
        in.close();
    } catch (IOException e) {
        Error += " post IO exception " + e;
        Log.d("SCOMM", Error);
        e.printStackTrace();
        return Error;
    }


    return sb.toString();


}
</code></pre>
