2

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
gnat
  • 6,213
  • 108
  • 53
  • 73
kehnar
  • 1,387
  • 2
  • 12
  • 25

1 Answers1

6

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

Community
  • 1
  • 1
alishaik786
  • 3,696
  • 2
  • 17
  • 25
  • Is HttpConnectionFactory is a default class or we have to download it from somewhere ? Cause for some reason my SDK says `it cannot be resolved to a type` – Cemre Mengü Nov 01 '12 at 08:24
  • 1
    I Put the link for getting that class. So see my post and get TWO classes from that link. – alishaik786 Nov 01 '12 at 10:33