1

I am connecting to a web service and did not set any connect timeout and read timeout on HttpURLConnection. What is the default connect timeout and read timeout? Is it dependent on the Android phone? Or does Android/ Java has its own default value for the timeout?

When I try to get the value of my connect timeout (getConnectTimeout()) and read timeout (getReadTimeout()), it returns a value of 0 which I assume is the equivalent of infinity. However, even though their value is 0, there are times that I'm still receiving a request timeout error. So their value can't be equal to infinity. Can someone enlighten me on this?

Arci
  • 6,647
  • 20
  • 70
  • 98

3 Answers3

3

Did you do any research? The defaults are detailed in the documentation. Here's the relevant parts:

public void setConnectTimeout (int timeout) Since: API Level 1

Sets the timeout value in milliseconds for establishing the connection to the resource pointed by this URLConnection instance. A SocketTimeoutException is thrown if the connection could not be established in this time. Default is 0 which stands for an infinite timeout.

public void setReadTimeout (int timeout) Since: API Level 1

Sets the timeout value in milliseconds for reading from the input stream of an established connection to the resource. A SocketTimeoutException is thrown if the connection could not be established in this time. Default is 0 which stands for an infinite timeout.

Also there is this note on timeouts:

URLConnection supports two timeouts: a connect timeout and a read timeout. By default, operations never time out.

It might be possible that the server you are connecting to is timing out your client.

Devin M
  • 9,636
  • 2
  • 33
  • 46
  • Yes. That is why I am confuse. I know that by default, the timeout is set to infinite. And yet, my request is still timing out. I am expecting that is should not timeout no matter what. – Arci Apr 03 '12 at 03:41
  • Thanks! After further research, I found out that it is indeed the server who is timing out the client. O_O – Arci May 08 '12 at 08:00
  • @Devin M : I am having problems with the connect timout and read timeout. They seem expire before the timeouts expire. Please see this question. Can you help me out with this question? http://stackoverflow.com/questions/12650127/sockettimeoutexception-before-the-connect-and-read-timeout-expires – Ashwin Sep 29 '12 at 06:22
0

The Javadoc for connect() is incorrect. Zero gives you the platform connect timeout, which is not infinite, but around a minute. You can only decrease this value with the timeout parameter to connect(), not increase it.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

If the getConnectTimeout and getReadTimeout from the object return zero, then that does indeed mean infinity. This is inherited from the underlying URLConnection class.

It may be that the exception you're receiving is not a SocketTimeoutException at all, you should verify this - it's possible to get IOException thrown regardless of the timeout settings.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953