除了@ astral-projection的解決方案,您可以簡單地聲明一個Socket
。我在我的許多項目中使用了以下代碼,並且超時確實可行。
Socket socket;
final String host = "www.google.com";
final int port = 80;
final int timeout = 30000; // 30 seconds
try {
socket = new Socket();
socket.connect(new InetSocketAddress(host, port), timeout);
}
catch (UnknownHostException uhe) {
Log.e("GoogleSock", "I couldn't resolve the host you've provided!");
}
catch (SocketTimeoutException ste) {
Log.e("GoogleSock", "After a reasonable amount of time, I'm not able to connect, Google is probably down!");
}
catch (IOException ioe) {
Log.e("GoogleSock", "Hmmm... Sudden disconnection, probably you should start again!");
}
雖然這可能會很棘手。正是在UnknownHostException
s時,可能需要更長的時間超時,約45秒 - 但在另一邊,這通常發生在你無法解析主機,這樣就會morelike意味着你的網絡連接已經missconfigured DNS解析(這是不太可能)。
無論如何,如果你想要對衝你的賭注,你可以通過兩種方式解決這個問題:
不要使用一臺主機,使用IP地址來代替。您可能會在主機上多次使用ping
多個Google的IP。例如:
[email protected] ~/services $ ping www.google.com
PING www.google.com (173.194.40.179) 56(84) bytes of data.
另一個解決方法將啓動一個線程WatchDog
並完成所需要的時間後,連接嘗試。顯然,強制結束意味着沒有成功,所以在你的情況下,Google會倒閉。
---- ----編輯
我加入的是如何將一個看門狗在這種情況下實施的示例。請記住,這是一種解決方案,甚至不需要發生,但如果您真的需要,它應該可以做到這一點。我要離開原來的代碼,所以你可能會看到不同之處:
Socket socket;
// You'll use this flag to check wether you're still trying to connect
boolean is_connecting = false;
final String host = "www.google.com";
final int port = 80;
final int timeout = 30000; // 30 seconds
// This method will test whether the is_connecting flag is still set to true
private void stillConnecting() {
if (is_connecting) {
Log.e("GoogleSock", "The socket is taking too long to establish a connection, Google is probably down!");
}
}
try {
socket = new Socket();
is_connecting = true;
socket.connect(new InetSocketAddress(host, port), timeout);
// Start a handler with postDelayed for 30 seconds (current timeout value), it will check whether is_connecting is still true
// That would mean that it won't probably resolve the host at all and the connection failed
// postDelayed is non-blocking, so don't worry about your thread being blocked
new Handler().postDelayed(
new Runnable() {
public void run() {
stillConnecting();
}
}, timeout);
}
catch (UnknownHostException uhe) {
is_connecting = false;
Log.e("GoogleSock", "I couldn't resolve the host you've provided!");
return;
}
catch (SocketTimeoutException ste) {
is_connecting = false;
Log.e("GoogleSock", "After a reasonable amount of time, I'm not able to connect, Google is probably down!");
return;
}
catch (IOException ioe) {
is_connecting = false;
Log.e("GoogleSock", "Hmmm... Sudden disconnection, probably you should start again!");
return;
}
// If you've reached this point, it would mean that the socket went ok, so Google is up
is_connecting = false;
注意:我假設你這樣做,你應該(我的意思是,不是在主UI),而這正在一個線程內進行(您可以使用AsyncTask,一個線程,一個服務內的線程......)。
來源
2014-01-30 23:06:31
nKn
你在哪裏建立http連接。 – Raghunandan
你可以輪詢一個連接的變化,簡單的廣播接收器。 – Skynet
我很好奇這個檢查的目的。請注意,如果看到網絡已啓動,則可以嘗試連接,但立即發現它斷開。 – seand