java.net.SocketException异常:操作超时问题,Android的?异常、操作、问题、net

由网友(哥丶看破繁华)分享简介:在我的应用程序,我写了code,用于连接到URL中包含In my application i wrote code for connecting to the URL like belowInputStream inputStream = new URL(url).openStream(); 我得到了erro...

在我的应用程序,我写了code,用于连接到URL中包含

In my application i wrote code for connecting to the URL like below

InputStream inputStream = new URL(url).openStream();    

我得到了error.Iam送我的logcat

i got the error.Iam sending my logcat

12-17 15:06:55.065: WARN/System.err(4952): java.net.SocketException: The operation timed out
12-17 15:06:55.065: WARN/System.err(4952):     at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
12-17 15:06:55.065: WARN/System.err(4952):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
12-17 15:06:55.065: WARN/System.err(4952):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
12-17 15:06:55.075: WARN/System.err(4952):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
12-17 15:06:55.075: WARN/System.err(4952):     at java.net.Socket.connect(Socket.java:1055)
12-17 15:06:55.075: WARN/System.err(4952):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
12-17 15:06:55.075: WARN/System.err(4952):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
12-17 15:06:55.075: WARN/System.err(4952):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
12-17 15:06:55.085: WARN/System.err(4952):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
12-17 15:06:55.085: WARN/System.err(4952):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1152)
12-17 15:06:55.085: WARN/System.err(4952):     at java.net.URL.openStream(URL.java:653)

如何解决这个问题

How to solve this problem

推荐答案

这是发生,因为有时你的服务器时间过长回应。其实这也可能发生由于速度较慢的网络,这样你就不能完全控制权。如果使用HttpClient,你可以增加超时时间:

This is happening because some times your server is taking too long to respond. Actually this could also happening due to a slow network, so you don't have full control over it. If you were using HttpClient, you could increase the timeout period:

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, WAIT_RESPONSE_TIMEOUT);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);

client = new DefaultHttpClient(httpParameters);

CONNECTION_TIMEOUT 是等待连接建立的时间。 WAIT_RESPONSE_TIMEOUT 是等待要接收数据的时间 - 在你的情况下,这是你需要增加

CONNECTION_TIMEOUT is the time to wait for a connection to establish. WAIT_RESPONSE_TIMEOUT is the time to wait for data to be received - in your case this is what you need to increase.

底线:

在停止使用URL和现在开始使用的HttpClient。 您所描述的情况是很常见的一种在生产中的应用,你需要照顾它。增加超时不会总是帮助,因为你的应用程序会出现暂停时,有一个速度较慢的网络。你可以添加一个重试机制或通知的请求失败的用户,让他再试一次。
阅读全文

相关推荐

最新文章