如何检查在黑莓在java中的互联网连接 [英] how to check internet connection in java in blackberry
问题描述
我要检查网络连接是否存在或者不黑莓设备,以便根据不同的结果,我可以调用Web服务获取数据,或从我的应用程序数据上传
我已经试过这一个。
CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS)))||
(CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B))!= FALSE
如果您要检查了互联网连接,然后发送任何URL到Web服务,并检查HTTP响应。如果HTT presponse是200那么只有您有互联网连接。做这样的.......
尝试
{
厂=新HttpConnectionFactory();
URL =这里放任何样品网址或任何Web服务来检查网络连接。
的HttpConnection = factory.getHttpConnection(URL);
响应= httpConnection.getResponse code();
如果(响应== HttpConnection.HTTP_OK)
{
回调(响应);
}其他
{
回调(响应);
}
}赶上(例外五)
{
的System.out.println(e.getMessage());
回调(0);
}
下面回应= 200,那么你有互联网连接。否则它是一个连接问题。您可以检查此像下面...........
公共无效回调(int i)以
{
如果(我== 200)
{
//你想做什么,就可以做什么。
}
其他
{
UiApplication.getUiApplication()的invokeLater(Runnable的新()
{
公共无效的run()
{
INT K = Dialog.ask(Dialog.D_OK,连接错误,请检查您的网络连接。);
如果(K == Dialog.D_OK)
{
System.exit(0);
}
}
});
}
}
下面System.exit(0);退出过你所在的应用程序。
把这些两个类
1) HttpConnectionFactory 的.java
2)的 HttpConnectionFactoryException 的.java
此链接:<一个href=\"http://stackoverflow.com/questions/8502915/tunnel-failed-exception-in-blackberry-curve-8520/8515091#8515091\">HttpConnection类
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
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
这篇关于如何检查在黑莓在java中的互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!