how to check internet connection in java in blackberry

大憨熊 提交于 2019-12-12 09:06:04

问题


I want to check whether internet connection is there or not in blackberry device so that depending on the result I can call webservices to get data or upload data from my application

I have tried this one

CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS))) ||
(CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B)) != false

回答1:


If you want to check the internet connection, then send any url to the web service and check the HTTP Response. If HTTPResponse is 200 then only you are having internet connection. Do like this.......

try
            {                   
                factory = new HttpConnectionFactory();
                url="Here put any sample url or any of your web service to check network connection.";
                httpConnection = factory.getHttpConnection(url);
                response=httpConnection.getResponseCode();
                if(response==HttpConnection.HTTP_OK)
                {
                    callback(response);
                }else
                {
                    callback(response);
                }
            } catch (Exception e) 
            {
                System.out.println(e.getMessage());
                callback(0);
            }

Here "response"=200 then you have an internet connection. otherwise it is a connection problem. You can check this like below...........

public void callback(int i)
{
    if(i==200)
    {
        //You can do what ever you want.                
    }
    else
    {
        UiApplication.getUiApplication().invokeLater(new Runnable() 
        {
            public void run() 
            {                   
                int k=Dialog.ask(Dialog.D_OK,"Connection error,please check your network connection..");
                if(k==Dialog.D_OK)
                {
                    System.exit(0);
                }
            }
        });
    }
}

Here System.exit(0); exit the application where ever you are.

Take these two classes

1)HttpConnectionFactory.java

2)HttpConnectionFactoryException.java

from this link:HttpConnection Classes



来源:https://stackoverflow.com/questions/7539044/how-to-check-internet-connection-in-java-in-blackberry

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