In my application i wrote code for connecting to the URL like below
InputStream inputStream = new URL(url).openStream();
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
Using try/catch/finally If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.
In the client code, after waiting for 15 seconds (or less), you can throw a new exception (using throws new Exception() ), but you have to remove the finally clause or else then the connection will close normally and no SocketException will be thrown.
The java. net. SocketException: Connection reset error usually comes when one of the parties in TCP connection like client or server is trying to read/write data, but other parties abruptly close the connection like it was crashed, stopped or terminated.
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
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.
Bottom line:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With