我们如何在黑莓应用程序中检查互联网连接是否可用?
当我向服务器发送请求时,我想检查设备连接的可用性。
答案 0 :(得分:2)
您可以通过此代码测试您的连接。
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;
}
答案 1 :(得分:1)
你可以利用HttpConnection.requestCode()方法检查连接是否可用。如果它返回200,意味着连接存在。试试这个,我用这段代码来检查连接,我不知道是否有任何连接其他方式。
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();
}
}
答案 2 :(得分:1)
您可以测试您是否覆盖了以下内容:
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;
}
}
如果您使用的是wifi设备,则需要单独检查wifi(我认为)
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;
}
}
我在我的应用中使用它们,它们似乎正常工作。