Android 中的 HttpURLConnection ,它们会自动重复直到连接成功吗?
HttpURLConnection in Android , will they repeat automatically until connection success?
这是 HttpURLConnection 代码,正在检查互联网连接。我只想知道它们会自动重复直到连接成功吗?还是我需要明确指定任何重试策略
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(500); //choose your own timeframe
urlc.setReadTimeout(500); //choose your own timeframe
urlc.connect();
int responseCode = urlc.getResponseCode();
if (responseCode == 200) {
//if internet is up then execute the jobs
doSomeWork();
} else {
Log.d("Warning", "Internet is not available");
}
HttpURLConnection 没有任何重试政策。你需要自己处理。
您可以使用 HttpURLConnection class 中的 getErrorStream() 方法检查错误,包括连接问题。如果未建立与服务器的连接,则它 returns 为空。
可在此处找到详细信息 - https://developer.android.com/reference/java/net/HttpURLConnection.html#getErrorStream()
这是 HttpURLConnection 代码,正在检查互联网连接。我只想知道它们会自动重复直到连接成功吗?还是我需要明确指定任何重试策略
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(500); //choose your own timeframe
urlc.setReadTimeout(500); //choose your own timeframe
urlc.connect();
int responseCode = urlc.getResponseCode();
if (responseCode == 200) {
//if internet is up then execute the jobs
doSomeWork();
} else {
Log.d("Warning", "Internet is not available");
}
HttpURLConnection 没有任何重试政策。你需要自己处理。
您可以使用 HttpURLConnection class 中的 getErrorStream() 方法检查错误,包括连接问题。如果未建立与服务器的连接,则它 returns 为空。
可在此处找到详细信息 - https://developer.android.com/reference/java/net/HttpURLConnection.html#getErrorStream()