how to check internet connection in java in blackb

2019-04-14 04:03发布

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条回答
三岁会撩人
2楼-- · 2019-04-14 04:38

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

查看更多
登录 后发表回答