java - HttpURLConnection very slow -
can spot why takes ~20 sec? running code below post json request 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"},"id":2002,"side":"left","role":"command"}'
curl on same box server running instant , server not complain.
a request android browser fast. have tried 2 android devices os version 4.x.
this question not help far can tell: android httpurlconnection slow
con.getinputstream() takes ~20 sec:
string httpjson(string url, jsonobject job) { string ret = null; httpurlconnection con = httpjsoncon(url); if(con!=null) httpjsoncon(con, url,job); homecoming ret; } httpurlconnection mkcon(string url) { httpurlconnection con = null; url u = null; seek { 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(); } grab (exception e) { log.w(tag, " e= " + e); if(con!=null) con.disconnect(); con = null; } homecoming con; } string sendjson(httpurlconnection con, jsonobject job) { string ret = null; if(con==null){ homecoming ret; } seek { final string towriteout = job.tostring(); final author 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())); } grab (ioexception e) { log.d(tag, " e= " + e); ret = null; } { if(con!=null) con.disconnect(); } homecoming ret; }
add request header specifies post content length.
con.setrequestproperty("content-length", "" + json.length());
java android
No comments:
Post a Comment