在我的应用程序中,我需要检查设备是否已连接到Internet。我尝试使用ConnectivityManager但它没有给出100%精确的结果。例如,我可能有一个Wi-Fi连接,但仍然无法访问互联网资源。在我的情况下,我必须打开VPN连接,在我连接到wi-fi之后,才能真正访问互联网。所以使用ConnectivityManager的方法不起作用。
所以,关于上述问题 - 我应该写一个手动的http请求以确保还是有另一种方式?
这是我正在使用的一些代码
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected() && cm.getActiveNetworkInfo().isAvailable();
答案 0 :(得分:3)
是的,你是对的,要检查你是否可以上网,你需要明确地测试它。如果您需要HTTP访问权限,可以尝试连接到以后想要连接的主机。
但是,您应该使用稍后要使用的连接方法。 HTTP可以工作,但不能FTP。因此,如果您需要FTP访问,则应进行测试。
如果您还想获得外部IP,可以使用此方法:
public static InetAddress getExternalIp() throws IOException {
URL url = new URL("http://automation.whatismyip.com/n09230945.asp");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Protocol", "Http/1.1");
connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("Keep-Alive", "1000");
connection.addRequestProperty("User-Agent", "Web-Agent");
Scanner s = new Scanner(connection.getInputStream());
try {
return InetAddress.getByName(s.nextLine());
} finally {
s.close();
}
}
如果您从此方法成功获得了IP地址,则可以通过HTTP连接到该地址。
答案 1 :(得分:2)
我认为不是每次都要检查互联网连接,而是将ConnectionTimeout
设置为HTTPRequest,这是最佳方式,
try
{
HttpGet request = new HttpGet(url));
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 60000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 60000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
// create object of DefaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
request.addHeader("Content-Type", "application/json");
HttpResponse response = httpClient.execute(request);
// get response entity
HttpEntity entity = response.getEntity();
// convert entity response to string
if (entity != null)
{
result = EntityUtils.toString(entity);
}
}
catch (SocketException e)
{
return "-222" + e.toString();
}
catch (Exception e)
{
return "-333" + e.toString();
}
答案 2 :(得分:0)
试试这个,
private boolean checkInternetConnection() {
try {
ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
nInfo.getActiveNetworkInfo().isConnectedOrConnecting();
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
return false; }
} catch (Exception e) {
return false;
}
}
它非常适合我...
希望有所帮助
答案 3 :(得分:0)
试试这个,
NetworkInfo ActiveNetwork;
String Isconnected="";
ConnectivityManager connectivitymanager;
connectivitymanager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Connection con=new Connection();
try
{
ActiveNetwork=connectivitymanager.getActiveNetworkInfo();
Isconnected="true";
}
catch(Exception error)
{
IsConnected="false";
}
答案 4 :(得分:0)
试试这个
private void MyCheckinternet() {
// TODO Auto-generated method stub
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
connected = true;
}
else
{
connected = false;
}
}