What is the default read and connect timeout for HttpURLConnection under android?
It's seam the default Timeout is 0, but now I m curious, is their any drawback to set the connection timeout to infinite? if something goes wrong can we have a connection that will forever wait ?
Due to the documentation of Java of the HttpURLConnection, timeout is set to 0 (which means infinity) by default and can be modified.
Specifically, it is written in the accessor/getter method in the documentation;
public int getConnectTimeout() Returns setting for connect timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).
Returns: an int that indicates the connect timeout value in milliseconds Since: 1.5 See Also: setConnectTimeout(int), connect()
If I were you, I would set the connection timeout before starting the connection and set my logic/flow based on my own initial values. Below, you can see an example for how to get the default value and set/modify the connection timeout parameter.
package com.levo.so.huc;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpConnectionTimeoutDemo {
public static void main(String[] args) throws IOException {
String url = "http://www.google.com/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
System.out.println("Default Connection Timeout : " + con.getConnectTimeout());
con.setConnectTimeout(1000);
System.out.println("New Connection Timeout : " + con.getConnectTimeout());
}
}
Default Connection Timeout : 0
New Connection Timeout : 1000
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