How can we check internet connection is available or not in a blackberry application?
When I send request to my server that time I want to check my device connection availability.
How can we check internet connection is available or not in a blackberry application?
When I send request to my server that time I want to check my device connection availability.
Hi u can test your connection by this code.
public static boolean isConnectionAvailable(String networkName) {
HttpConnection connection;
try {
HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory("http://m.google.com");
connection = httpConnectionFactory.makeConnectionUsing(networkName);
if(connection == null)
return false;
else if (connection.getResponseCode() == HttpConnection.HTTP_OK)
{
connection.close();
return true;
}
connection.close();
} catch( Exception e) {}
return false;
}
you can make use of HttpConnection.requestCode() method to check whether connection is available or not.If it returns 200, means connection exists.Try this, i used this code to check connection, i dont know if there is any other way.
public void run() {
try{
HttpConnection conn = (HttpConnection)Connector.open("http://www.goole.com");
conn.setRequestMethod(HttpConnection.GET);
int i = conn.getResponseCode();
if(conn.getResponseCode()==HttpConnection.HTTP_OK)
{
System.out.println("----------------------------------------responsecode-------------------->>>>>>: " + conn.getResponseCode());
_screen.requestSuccess("Connection Available. ResponseCode:" + i);
conn.close();
}
else
{
System.out.println("----------------------------------------responsecode-------------------->>>>>>: " + conn.getResponseCode());
_screen.requestFailed("Connection Not Available.ResponseCode:" + i);
conn.close();
}
}
You can test if you coverage with something like this:
public static boolean hasSignal() {
if (RadioInfo.getState() == RadioInfo.STATE_OFF || RadioInfo.getSignalLevel() == RadioInfo.LEVEL_NO_COVERAGE) {
System.out.println(" -- no signal");
return false;
} else {
return true;
}
}
If you are using a wifi device you need to check wifi separately (i think)
public static boolean hasWifi() {
if ( ( RadioInfo.getActiveWAFs() & RadioInfo.WAF_WLAN ) != 0 ) {
return (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT, RadioInfo.WAF_WLAN, true));
} else {
System.out.println(" -- no wifi");
return false;
}
}
I use those in my app and they seem to work correctly.