HttpURLConnection very slow
<p>Can anyone spot why this takes ~20 sec?
I am running the code below to post a JSON request to a local server 192.168.1.127.</p>

<blockquote>
  <p>curl -H "Content-type: application/json" -X POST
  http:// 192.168.1.127:8080/bed -d
  '{"command":{"value":3.012,"set":"target_pressure_voltage"},"id":2002,"side":"left","role":"command"}'</p>
</blockquote>

<p>curl on the same box where the server is running is instant and the server does not complain.</p>

<p>A get request from the Android browser is fast. I have tried two Android devices with os version 4.x. </p>

<p>This question does not help as far as I can tell:
<a href="http://stackoverflow.com/questions/12108166/android-httpurlconnection-very-slow">Android HttpURLConnection VERY slow</a></p>

<p><strong>con.getInputStream()</strong> Takes ~20 sec:</p>

<pre><code>    String httpJson(String url, JSONObject job) {
    String ret = null;      
        HttpURLConnection con = httpJsonCon(url);
        if(con!=null)
            httpJsonCon(con, url,job);      
    return ret;

}



 HttpURLConnection mkCon(String url) {
    HttpURLConnection con = null;
    URL u = null;
    try {
        u = new URL(url);
        con = (HttpURLConnection) (u.openConnection());

        con.setRequestMethod("POST");
        con.setUseCaches(false);
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Accept", "text/plain");
        con.setRequestProperty("Accept", "application/json");
        con.setDoOutput(true);
        con.setDoInput(true);
        con.connect();

    } catch (Exception e) {
        Log.w(TAG, " e= " + e);
        if(con!=null)
            con.disconnect();

        con = null;
    }

    return con;
}

String sendJson(HttpURLConnection con, JSONObject job) {
    String ret = null;
    if(con==null){
        return ret;
    }

    try {
        final String toWriteOut = job.toString();

        final Writer out = new OutputStreamWriter(new BufferedOutputStream(
                con.getOutputStream()), "UTF-8");
        out.write(toWriteOut);
        out.flush();
        //con.getInputStream() Takes ~20 sec:
        final BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));  



    } catch (IOException e) {

        Log.d(TAG, " e= " + e);

        ret = null;
    } finally {
        if(con!=null)
            con.disconnect();
    }

    return ret;

}
</code></pre>
