我的功能会根据互联网的可用性返回 true 或 false :
private boolean isNetworkAvailable(String url, int timeout) {
try{
URL myUrl = new URL(url);
URLConnection connection = myUrl.openConnection();
connection.setConnectTimeout(timeout);
connection.connect();
return true;
} catch (Exception e) {
return false;
}
它在模拟器上运行正常,但即使互联网在手机上运行,它也会在Android手机上返回false!
答案 0 :(得分:0)
首先,您应该将“Exception e”更改为“IOException e”。 然后检查您调用此方法的位置。你不应该在主线程中打电话。