How to check for an active internet connection in blackberry SDK?

风流意气都作罢 提交于 2019-12-08 09:14:42

问题


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.


回答1:


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;
}



回答2:


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();


        }

    }



回答3:


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.



来源:https://stackoverflow.com/questions/7857838/how-to-check-for-an-active-internet-connection-in-blackberry-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!