I am copying this method verbatim from my application, which isn't complete yet but it does attempt to provide me with a timeout stack trace if things don't go smoothly:
protected boolean isHttpAlive() {
boolean isHttpOk = false;
HttpURLConnection httpConnection = null;
try {
URL gurl = new URL("http://www.amazon.com/");
URLConnection connection = gurl.openConnection();
connection.setConnectTimeout(5 * 1000); // 5 seconds!
httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK)
isHttpOk = true;
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (httpConnection != null)
httpConnection.disconnect();
}
return isHttpOk;
}
Now on one of my test devices (Droid), when there is a problem, I do get the exception but only after 6 minutes and 36 seconds, not 5 seconds as I set in the code above.
The timeout exception is thrown for the getResponseCode()
.
Why?
What am I missing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…