HttpURLConnection is very slow

Can anyone understand why this takes ~ 20 seconds? I am running the code below to send a JSON request to the local server 192.168.1.127.

curl -H "Content-type: application / json" -X POST http://192.168.1.127:8080/bed -d {"Command": {"value": 3,012, "set": "target_pressure_voltage"}, " identifier ": 2002," side ":" left "," role ":" team "} '

curl in the same field where the server is running instantly, and the server does not complain.

Get a request from an Android browser quickly. I tried two Android devices with os version 4.x.

This question does not help, as far as I can tell: Android HttpURLConnection is VERY slow

con.getInputStream () takes ~ 20 seconds:

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; } 
0
java android
source share
1 answer

Add a request header that indicates the length of the message content.

 con.setRequestProperty("Content-Length", "" + json.length()); 
0
source

All Articles