如何在黑莓中检查java中的互联网连接

时间:2011-09-24 12:35:43

标签: blackberry java-me

我想检查黑莓设备中是否存在互联网连接,以便根据结果我可以调用webservices来获取数据或从我的应用程序上传数据

我试过这个

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

1 个答案:

答案 0 :(得分:6)

如果要检查互联网连接,请将任何网址发送到网络服务并检查HTTP响应。如果HTTPResponse是200,那么只有你有互联网连接。这样做.......

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

这里“响应”= 200然后你有一个互联网连接。否则是连接问题。您可以像下面这样检查...........

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

这里是System.exit(0);随时随地退出应用程序。

选择两个

1) HttpConnectionFactory .java

2) HttpConnectionFactoryException .java

来自此链接:HttpConnection Classes