灵通163百科在线-实用的手机,证券免费资源网站

您现在的位置:首页 > 塞班Symbian > 软件工具 >

Android的java.net.SocketTimeoutException:连接超时

教程来源:www.lt163.com     点击数:     更新时间:2017-12-07 07:02

Recently I have encounter the following error in my program:

11-18 12:30:30.259: W/System.err(21368): java.net.SocketTimeoutException: Connection timed out 11-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.<init>(HttpConnection.java:75) 11-18 12:30:30.259: W/System.err(21368): at org.apache.harmony.luni.internal.net.<init>(HttpConnection.java:48) 11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.$Address.connect(HttpConnection.java:322) 11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.(HttpConnectionPool.java:89) 11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.(HttpURLConnectionImpl.java:285) 11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.(HttpURLConnectionImpl.java:267) 11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.(HttpURLConnectionImpl.java:205) 11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.(HttpURLConnectionImpl.java:614) 11-18 12:30:30.269: W/System.err(21368): at com.example.simplevider.SimpleVideo$4.run(SimpleVideo.java:122) 11-18 12:30:30.279: W/System.err(21368): at java.lang.Thread.run(Thread.java:1019)

The function that created this error is as follows :

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(); }

This function is perfectly working in normal JAVA but when copied to Android it casts java.net.SocketTimeoutException: Connection timed out and I don't know why.

解决方案

There are two possibilities,

1)have you checked and tested your connection.

2)better don't set any connection timeout,if you are setting chose maximum time,,bcos it throws an error,if server didn't response within given time..

本文地址:IT屋 » Android java.net.SocketTimeoutException: Connection timed out

问 题

最近,我遇到下面的错误在我的计划:

12月11日至18日:30:30.259:W / System.err的(21368):java.net.SocketTimeoutException:连接超时
十二月11日至18日:30:30.259:W / System.err的(21368):在org.apache.harmony.luni.platform.OSNetworkSystem.connect(本机方法)
十二月11日至18日:30:30.259:W / System.err的(21368):在dalvik.system.BlockGuard $ WrappedNetworkSystem.connect(BlockGuard.java:357)
十二月11日至18日:30:30.259:W / System.err的(21368):在org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
十二月11日至18日:30:30.259:W / System.err的(21368):在org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
十二月11日至18日:30:30.259:W / System.err的(21368):在java.net.Socket.connect(Socket.java:1002)
十二月11日至18日:30:30.259:W / System.err的(21368):在org.apache.harmony.luni.internal.net.<init>(HttpConnection.java:75)
十二月11日至18日:30:30.259:W / System.err的(21368):在org.apache.harmony.luni.internal.net.<init>(HttpConnection.java:48)
十二月11日至18日:30:30.269:W / System.err的(21368):在org.apache.harmony.luni.internal.net.$Address.connect(HttpConnection.java:322)
十二月11日至18日:30:30.269:W / System.err的(21368):在org.apache.harmony.luni.internal.net.(HttpConnectionPool.java:89)
十二月11日至18日:30:30.269:W / System.err的(21368):在org.apache.harmony.luni.internal.net.(HttpURLConnectionImpl.java:285)
十二月11日至18日:30:30.269:W / System.err的(21368):在org.apache.harmony.luni.internal.net.(HttpURLConnectionImpl.java:267)
十二月11日至18日:30:30.269:W / System.err的(21368):在org.apache.harmony.luni.internal.net.(HttpURLConnectionImpl.java:205)
十二月11日至18日:30:30.269:W / System.err的(21368):在org.apache.harmony.luni.internal.net.(HttpURLConnectionImpl.java:614)
十二月11日至18日:30:30.269:W / System.err的(21368):在com.example.simplevider.SimpleVideo $ 4.run(SimpleVideo.java:122)
十二月11日至18日:30:30.279:W / System.err的(21368):在java.lang.Thread.run(Thread.java:1019)
没有相关教程
UC浏览器 最先进实用的手机端浏览器,最大限度节省页面浏览流量,点击下载
老虎地图 绿色纯免费GPS定位省流量的最佳手机地图,点击下载
网秦手机安全系列 手机杀毒防盗,防火墙,构建手机安全,点击进入
塞班精品游戏 最流行最给力的塞班S60免签名名精品游戏合集,点击进入
阿里通 手机上轻松拨打网络电话和收发短信,高音质超省话费,资费标准
爱科手机电话 国内最好的手机网络电话领先品牌,价格低廉,点击查看
愤怒的小鸟2 全民挑战超流行休闲游戏力推新作-愤怒的小鸟2,点击查看
魔力旺旺 淘宝卖家首选的交易工具

资源下载排行
文章阅读排行