开发者

how to check internet connection in java in blackberry

开发者 https://www.devze.com 2023-04-08 16:24 出处:网络
I want to check whether internet connection is there or not in blackberry device so that depending on the result I can call we开发者_开发问答bservices to get data or upload datafrom my application

I want to check whether internet connection is there or not in blackberry device so that depending on the result I can call we开发者_开发问答bservices 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号