私は次のコードブロックを持っています:
try {
URL url = new URL("http://site-to-test.com/nonexistingpage.html");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", CoreProtocolPNames.USER_AGENT);
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(500); // timeout is in milliseconds
urlc.connect();
if (urlc.getResponseCode() == 404) {
// Server was reachable
Log.i(TAG, "Server is reachable");
}
} catch (MalformedURLException mue) {
Log.e(TAG, "MalformedURLException: " + mue.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
}
site-to-test
現在のネットワークを介してドメインに到達できない場合、このコードはIOException
. そして、特にタイムアウト値を 500ms に設定しました。ここで何が欠けていますか?ネットワークの状態とサイトの可用性に関係なく、上記のブロックは 0.5 秒で終了するべきではありませんか?