4
回答
httpconnection.getOutputStream()在2G或3G网络中超时
华为云4核8G,高性能云服务器,免费试用   

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




<无标签>
举报
doliangzhe
发帖于4年前 4回/286阅
顶部