Android java.net.SocketTimeoutException:连接超时

Android java.net.SocketTimeoutException:连接超时,第1张

概述最近,我在程序中遇到以下错误:11-1812:30:30.259:W/System.err(21368):java.net.SocketTimeoutException:Connectiontimedout11-1812:30:30.259:W/System.err(21368):atorg.apache.harmony.luni.platform.OSNetworkSystem.connect(NativeMethod)11-1812:30:30.25

最近,我在程序中遇到以下错误:

11-18 12:30:30.259: W/System.err(21368): java.net.socketTimeoutException: Connection timed out11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)11-18 12:30:30.259: W/System.err(21368):    at dalvik.system.BlockGuard$WrappednetworkSystem.connect(BlockGuard.java:357)11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)11-18 12:30:30.259: W/System.err(21368):    at java.net.socket.connect(Socket.java:1002)11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.httpconnection.<init>(httpconnection.java:75)11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.httpconnection.<init>(httpconnection.java:48)11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.httpconnection$Address.connect(httpconnection.java:322)11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.httpconnectionPool.get(httpconnectionPool.java:89)11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.gethttpconnection(httpURLConnectionImpl.java:285)11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.makeConnection(httpURLConnectionImpl.java:267)11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.connect(httpURLConnectionImpl.java:205)11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.getoutputStream(httpURLConnectionImpl.java:614)11-18 12:30:30.269: W/System.err(21368):    at com.example.simplevIDer.SimpleVIDeo.run(SimpleVIDeo.java:122)11-18 12:30:30.279: W/System.err(21368):    at java.lang.Thread.run(Thread.java:1019)

产生此错误的函数如下:

    private voID sendStuff() {    Log.e("sendStuff", "======================================");    new Thread(new Runnable() {        @OverrIDe        public voID run() {            final int BUFFER_SIZE = 4096;            try {                file uploadfile = new file(existingfilename);                System.out.println("file to upload: " + existingfilename);                URL url = new URL(URL);                httpURLConnection httpConn = (httpURLConnection) url.openConnection();                httpConn.setUseCaches(false);                httpConn.setDoOutput(true);                httpConn.setRequestMethod("POST");                httpConn.setReadTimeout(60*1000);                httpConn.setConnectTimeout(60 * 1000);                // sets file name as a http header                httpConn.setRequestProperty("filename", uploadfile.getname());                httpConn.setRequestProperty("extra-ID", uploadfile.getabsolutefile().toString());                httpConn.setRequestProperty("extra-ID2", uploadfile.getParent());                httpConn.setRequestProperty("extra-ID3", uploadfile.length() + "");                OutputStream outputStream = httpConn.getoutputStream(); //<< this is the source of the error                fileinputStream inputStream = new fileinputStream(uploadfile);                byte[] buffer = new byte[BUFFER_SIZE];                int bytesRead = -1;                System.out.println("Start writing data...");                while ((bytesRead = inputStream.read(buffer)) != -1) {                    outputStream.write(buffer, 0, bytesRead);                }                System.out.println("Data was written.");                outputStream.close();                inputStream.close();                // always check http response code from server                int responseCode = httpConn.getResponseCode();                if (responseCode == httpURLConnection.http_OK) {                    // reads server's response                    BufferedReader reader = new BufferedReader(new inputStreamReader(httpConn.getinputStream()));                    String response = reader.readline();                    System.out.println("Server's response: " + response);                } else {                    System.out.println("Server returned non-OK code: " + responseCode);                }            } catch (Exception e) {                e.printstacktrace();                sendStuff();            }        }    }).start();}

此函数在正常的JAVA中可以正常使用,但是当复制到AndroID时,它会抛出java.net.socketTimeoutException:连接超时,我不知道为什么.

解决方法:

有两种可能性,

1)您是否检查并测试了连接.

2)最好不要设置任何连接超时,如果您设置了选择的最大时间,则bcos会引发错误,如果服务器在给定时间内没有响应.

总结

以上是内存溢出为你收集整理的Android java.net.SocketTimeoutException:连接超时全部内容,希望文章能够帮你解决Android java.net.SocketTimeoutException:连接超时所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1095615.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存