我在使用httpconnection.getOutputStream()这个方法时,在wifi条件下是没问题,但是使用2G或3G网络下会报java.net.SocketTimeoutException异常,设置了较大的超时时间,有没有好的建议?
代码如下:
private boolean doPost() { try { URL url = new URL(this.url); httpconnection = (HttpURLConnection)url.openConnection(); httpconnection.setRequestMethod("POST"); httpconnection.setDoInput(true); httpconnection.setDoOutput(true); httpconnection.setUseCaches(false); httpconnection.setConnectTimeout(maxTime); httpconnection.setReadTimeout(maxTime); httpconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpconnection.setRequestProperty("Content-Length", String.valueOf(parameter.getBytes().length)); httpconnection.setRequestProperty("Connection", "Keep-Alive"); DataOutputStream dos = new DataOutputStream(httpconnection.getOutputStream()); dos.write(parameter.getBytes()); dos.flush(); dos.close(); } catch (Exception e) { e.printStackTrace();
DataOutputStream dos = new DataOutputStream(httpconnection.getOutputStream());这一句报错,错误信息如下:
07-08 16:09:44.000: W/System.err(11627): java.net.SocketTimeoutException: failed to connect to www.ilife999.com/119.137.34.3 (port 8080) after 60000ms 07-08 16:09:44.027: W/System.err(11627): at libcore.io.IoBridge.connectErrno(IoBridge.java:159) 07-08 16:09:44.031: W/System.err(11627): at libcore.io.IoBridge.connect(IoBridge.java:112) 07-08 16:09:44.031: W/System.err(11627): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 07-08 16:09:44.035: W/System.err(11627): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 07-08 16:09:44.039: W/System.err(11627): at java.net.Socket.connect(Socket.java:842) 07-08 16:09:44.039: W/System.err(11627): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76) 07-08 16:09:44.043: W/System.err(11627): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 07-08 16:09:44.043: W/System.err(11627): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341) 07-08 16:09:44.046: W/System.err(11627): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 07-08 16:09:44.046: W/System.err(11627): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 07-08 16:09:44.050: W/System.err(11627): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) 07-08 16:09:44.054: W/System.err(11627): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310) 07-08 16:09:44.054: W/System.err(11627): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 07-08 16:09:44.054: W/System.err(11627): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 07-08 16:09:44.058: W/System.err(11627): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 07-08 16:09:44.058: W/System.err(11627): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188) 07-08 16:09:44.058: W/System.err(11627): at com.jr.remotecontrolhome.http.JRHttpConnect.doPost(JRHttpConnect.java:242) 07-08 16:09:44.062: W/System.err(11627): at com.jr.remotecontrolhome.http.JRHttpConnect.doConnect(JRHttpConnect.java:155) 07-08 16:09:44.066: W/System.err(11627): at com.jr.remotecontrolhome.http.JRHttpConnect.connect(JRHttpConnect.java:132) 07-08 16:09:44.070: W/System.err(11627): at com.jr.remotecontrolhome.http.JRHttpConnect.startConnect(JRHttpConnect.java:119) 07-08 16:09:44.070: W/System.err(11627): at com.jr.remotecontrolhome.http.JRHttpConnect.post(JRHttpConnect.java:83) 07-08 16:09:44.070: W/System.err(11627): at com.jr.remotecontrolhome.LoginActivity$3.run(LoginActivity.java:122)
把超时时间设置的长一点再试试。多谢。我设置到30秒依然不行我用的是GPRS大哥,这个问题你解决了没,我也遇到这个问题,郁闷死了,在2G和3G网络下连接超时
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。