开发者社区> 问答> 正文

httpconnection.getOutputStream()在2G或3G网络中超时?报错

我在使用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)




展开
收起
爱吃鱼的程序员 2020-06-15 19:44:12 679 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    把超时时间设置的长一点再试试。多谢。我设置到30秒依然不行我用的是GPRS大哥,这个问题你解决了没,我也遇到这个问题,郁闷死了,在2G和3G网络下连接超时

    2020-06-15 19:44:29
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Session:更加安全、可靠的数据中心网络产品更新 立即下载
Session:极简易用的全球化网络产品更新 立即下载
Session:弹性、高可用、可观测的应用交付网络产品更新 立即下载