在模拟器上工作但不在Android手机上的方法

时间:2014-12-14 19:30:02

标签: android android-emulator internet-connection

我的功能会根据互联网的可用性返回 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!

1 个答案:

答案 0 :(得分:0)

首先,您应该将“Exception e”更改为“IOException e”。 然后检查您调用此方法的位置。你不应该在主线程中打电话。