6

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

3
  • what's the url? Are you sure your server is up? Commented Dec 17, 2010 at 12:36
  • is the server reachable and does it accept connections? Commented Dec 17, 2010 at 12:36
  • yes my server is up and this error comes some times only Commented Dec 17, 2010 at 12:57

2 Answers 2

13

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:

  • Stop using URL and start using HttpClient now.
  • The situation you are describing is a very common one in a production application and you need to cater for it. Increasing the timeout won't always help, as your application will appear to halt when there is a slow network. You could add a retry mechanism or inform the user that the request has failed and ask him to try again.
Sign up to request clarification or add additional context in comments.

Comments

0

Also, I had the case that you are passing a wrong url, and the app is looking to retrieve the data, but never finds it. Check your url, maybe if you correct it, just in case that it would be wrong, then the request would not throws you that error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.