我正在从我的应用程序中逐字复制此方法,该方法尚未完成,但如果事情进展不顺利,它确实会尝试为我提供超时堆栈跟踪:
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;
}
现在在我的一个测试设备(Droid)上,当出现问题时,我确实得到了异常,但只是在6 分 36 秒之后,而不是我在上面的代码中设置的 5 秒。
为getResponseCode()
.
为什么?
我错过了什么?