0

I have a java program that gather data from the web. Unfortunately, my network has a problem and it goes off and on.
I need a trick to ask my program wait until the connection is on again and continue its job.
I use the "URLConnection" library to connect. I made a loop to reconnect when it a "ConnectException" is catched, but it doesn't work.
Any suggestions?

3
  • Please post your connection code and tell us what exactly "doesn't work". Commented May 5, 2012 at 17:57
  • Give us the exceptions you're getting. Commented May 5, 2012 at 17:57
  • Now, it is connected and I cannot test it, I should wait! sorry Commented May 5, 2012 at 18:28

1 Answer 1

1

my network has a problem and it goes off and on. I need a trick to ask my program wait until the connection is on again and continue its job

It depends on what your program's purpose is.
How long are these intermittent failures? If they are short enough you could use setConnectTimeout(0) to indicate an infinite timeout period while trying to connect but if your program has to report something back then it is not a good option for the end user.
You could set a relatively low timeout so that when you start to lose the network you will get a java.net.SocketTimeoutException.
When you catch this, you could wait for a period and try again in a loop e.g. for 3 times and then perhaps report a failure.
It depends on what you are trying to do.

Sign up to request clarification or add additional context in comments.

2 Comments

There is no setTimeout() method at the link you supplied.
Corrected it. It is setConnectTimeout

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.