HttpURLConnection的速度很慢 [英] HttpURLConnection very slow
问题描述
任何人都可以发现为什么这大约需要20秒?
我运行下面的code张贴JSON请求到本地服务器192.168.1.127。
卷曲-H内容类型:应用程序/ JSON-X POST
HTTP:// 192.168.1.127:8080/bed -d
'{\"command\":{\"value\":3.012,\"set\":\"target_$p$pssure_voltage\"},\"id\":2002,\"side\":\"left\",\"role\":\"command\"}'
块引用>在服务器运行相同的方块卷曲是即时的,并且服务器不抱怨。
从Android浏览器的GET请求快。我已经试过两次与OS 4.x版本的Android设备
这个问题没有帮助,据我可以告诉:
的Android HttpURLConnection的速度很慢con.getInputStream(),大约需要20秒:
字符串httpJson(字符串URL,JSONObject的工作){
字符串RET = NULL;
HttpURLConnection类CON = httpJsonCon(URL);
如果(CON!= NULL)
httpJsonCon(CON,网址,工作);
返回RET;} HttpURLConnection的mkCon(字符串URL){
HttpURLConnection类CON = NULL;
URL U = NULL;
尝试{
U =新的URL(网址);
CON =(HttpURLConnection类)(u.openConnection()); con.setRequestMethod(POST);
con.setUseCaches(假);
con.setRequestProperty(内容类型,应用/ JSON);
con.setRequestProperty(接受,text / plain的);
con.setRequestProperty(接受,应用/ JSON);
con.setDoOutput(真);
con.setDoInput(真);
con.connect(); }赶上(例外五){
Log.w(TAG,E =+ e)条;
如果(CON!= NULL)
con.disconnect(); CON = NULL;
} 返回CON;
}字符串sendJson(HttpURLConnection类骗子,JSONObject的工作){
字符串RET = NULL;
如果(CON == NULL){
返回RET;
} 尝试{
最后弦乐toWriteOut = job.toString(); 最终作家出=新OutputStreamWriter(新的BufferedOutputStream(
con.getOutputStream()),UTF-8);
out.write(toWriteOut);
了out.flush();
//con.getInputStream(),大约需要20秒:
在=新的BufferedReader(新的InputStreamReader(con.getInputStream()))最终的BufferedReader; }赶上(IOException异常五){ Log.d(TAG,E =+ e)条; RET = NULL;
} {最后
如果(CON!= NULL)
con.disconnect();
} 返回RET;}
解决方案添加请求头指定的帖子内容长度。
con.setRequestProperty(内容长度,+ json.length());
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.
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 the same box where the server is running is instant and the server does not complain.
A get request from the Android browser is fast. I have tried two Android devices with os version 4.x.
This question does not help as far as I can tell: Android HttpURLConnection VERY 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); 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; }
解决方案Add a request header that specifies the post content length.
con.setRequestProperty("Content-Length", "" + json.length());
这篇关于HttpURLConnection的速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!