我有一个方法来检查互联网连接,在device.The方法总是抛出异常,即使手机是连接到wifi/gprs。我需要在xml中添加任何权限吗?还是代码出错了?
public boolean checkConnectivity(String url) {
try{
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
conn.setRequestMethod("HEAD");
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return true;
}else{
return false;
}
}catch(Exception e) {
System.out.println("myhttp exception");
return false;
}
}发布于 2012-05-09 13:45:22
将以下内容添加到清单文件中:
<uses-permission android:name="android.permission.INTERNET" />发布于 2012-05-09 13:54:12
您需要在Android Manifest文件中添加以下权限:
<uses-permission android:name="android.permission.INTERNET" />此外,我还建议使用Connectivity Manager Service来执行此http://developer.android.com/reference/android/net/ConnectivityManager.html操作
https://stackoverflow.com/questions/10510437
复制相似问题