Reputation: 4287
In my application I need to check whether device is connected to Internet. I tried using ConnectivityManager but it doesn't give a 100% precise result. For instance, I might have a wi-fi connection but still don't have access to internet resources. In my case I've got to open a VPN connection, after I've connected to via wi-fi, in order to get real access to Internet. So the approach with ConnectivityManager doesn't work.
So, regarding the above - should I write a manual http request in order to ensure or there's another way ?
Here's some code I'm using
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected() && cm.getActiveNetworkInfo().isAvailable();
Upvotes: 3
Views: 1980
Reputation: 109257
Instead of checking every time for internet connection, I think setting ConnectionTimeout
to HTTPRequest its the best way.
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();
}
Upvotes: 2
Reputation:
try this
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;
}
}
Upvotes: 0
Reputation: 7031
Try with this,
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";
}
Upvotes: 0
Reputation: 10079
Try this,
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;
}
}
It working perfectly for me...
Hope it helps
Upvotes: 0
Reputation: 43514
Yes, you are correct, to check that you can reach the internet you need to test that explicitly. If you need HTTP access you can try connecting to the host you later on want to connect to.
You should however use the connection method that you intend to use later on. HTTP can work but not FTP. So if you need FTP access you should test it.
If you also want to get the external IP you can use this method:
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();
}
}
If you successfully got an IP address back from this method, you can connect via HTTP to that.
Upvotes: 3