启用多个连接类型 [英] Enabling Multiple Connection Type
本文介绍了启用多个连接类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的应用程序HTTP连接,当前时间是,如果用户连接到WIFI连接,它只能适用。我会怎么做,如果我想启用它为特定设备有什么联系?说,该设备具有BIS服务,甚至是一个正常的WAP服务。
这是我的code。
尝试
{
连接=(HttpConnection的)Connector.open(URL +接口=无线);
是= connection.openInputStream();
尝试
{
SAXParser的解析器= SAXParserFactory.newInstance()newSAXParser()。
parser.parse(是,位于RSSHandler);
}
赶上(的ParserConfigurationException E)
{
e.printStackTrace();
}
赶上(SAXException的E)
{
e.printStackTrace();
}
赶上(IOException异常E)
{
e.printStackTrace();
}
} 赶上(IOException异常E)
{
e.printStackTrace();
Dialog.inform(坏URL);
}
解决方案
使用以下code,以获得连接字符串
/ **
*决定什么连接方式使用,并返回到使用它的必要串。
返回:与连接信息的字符串
* /
私人静态字符串getConnectionString()
{
//这个code是根据AccelGolf的迈克·尼尔森开发的连接code。
// http://blog.accelgolf.com/2009/05/22/blackberry-cross-carrier-and-cross-network-http-connection
字符串的connectionString = NULL; //模拟器行为由USE_MDS_IN_SIMULATOR变量控制。
如果(DeviceInfo.isSimulator())
{
如果(UploaderThread.USE_MDS_IN_SIMULATOR)
{
的LogMessage(设备是一个模拟器和USE_MDS_IN_SIMULATOR是真的);
的connectionString =; deviceside =假;
}
其他
{
的LogMessage(设备是一个模拟器和USE_MDS_IN_SIMULATOR是假的);
的connectionString =; deviceside =真正的;
}
} //无线是preferred传输方法
否则如果(WLANInfo.getWLANState()== WLANInfo.WLAN_STATE_CONNECTED)
{
的LogMessage(设备通过Wifi连接。);
的connectionString =;接口=无线网络;
} //是运营商网络连接的唯一途径?
否则如果((CoverageInfo.getCoverageStatus()及CoverageInfo.COVERAGE_DIRECT)== CoverageInfo.COVERAGE_DIRECT)
{
的LogMessage(运营商的覆盖范围。); 串carrierUid = getCarrierBIBSUid();
如果(carrierUid == NULL)
{
//有载体的覆盖面,但不青苗。因此,使用运营商的网络TCP
的LogMessage(否UID);
的connectionString =; deviceside =真正的;
}
其他
{
//否则,使用UID来构造一个有效的载体青苗要求
的LogMessage(uid是:+ carrierUid);
的connectionString =; deviceside = FALSE; connectionUID =+ carrierUid +; ConnectionType = MDS-公开;
}
} //检查的MDS连接,而不是(黑莓企业服务器)
否则如果((CoverageInfo.getCoverageStatus()及CoverageInfo.COVERAGE_MDS)== CoverageInfo.COVERAGE_MDS)
{
的LogMessage(MDS报道发现);
的connectionString =; deviceside =假;
} //如果没有可用的连接中断,以避免窃听unnecssarily用户。
否则如果(CoverageInfo.getCoverageStatus()== CoverageInfo.COVERAGE_NONE)
{
的LogMessage(没有可用的连接。);
} //从理论上讲,所有的基地覆盖,这不应该是到达。
其他
{
的LogMessage(没有其他选择中,假定设备。);
的connectionString =; deviceside =真正的;
} 返回的connectionString;
}/ **
*看起来通过为运营商提供的网络青苗手机的服务预订
返回:用于连接到该网络的UID。
* /
私人静态字符串getCarrierBIBSUid()
{
ServiceRecord [] =记录ServiceBook.getSB()getRecords()。
INT currentRecord; 对于(currentRecord = 0; currentRecord< records.length; currentRecord ++)(。记录[currentRecord] .getCid()与toLowerCase()等于(IPPP)){IF {如果(记录[currentRecord] .getName()。 。与toLowerCase()的indexOf(围兜)> = 0)
{
返回记录[currentRecord] .getUid();
}
}
} 返回null;
}
I have a HTTP Connection on my application, for the current time being it is only applicable if the users are connected to WIFI connection. How would I do this if I'd like to enable it for any connections that the particular device has? Say that the device has a BIS service, or even a normal WAP service.
This is my code.
try
{
connection = (HttpConnection) Connector.open(url+ ";interface=wifi");
is = connection.openInputStream();
try
{
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(is, rssHandler);
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
catch (IOException e)
{
e.printStackTrace();
Dialog.inform("Bad URL");
}
解决方案
Use the following code to get the connection string
/**
* Determines what connection type to use and returns the necessary string to use it.
* @return A string with the connection info
*/
private static String getConnectionString()
{
// This code is based on the connection code developed by Mike Nelson of AccelGolf.
// http://blog.accelgolf.com/2009/05/22/blackberry-cross-carrier-and-cross-network-http-connection
String connectionString = null;
// Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
if(DeviceInfo.isSimulator())
{
if(UploaderThread.USE_MDS_IN_SIMULATOR)
{
logMessage("Device is a simulator and USE_MDS_IN_SIMULATOR is true");
connectionString = ";deviceside=false";
}
else
{
logMessage("Device is a simulator and USE_MDS_IN_SIMULATOR is false");
connectionString = ";deviceside=true";
}
}
// Wifi is the preferred transmission method
else if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
{
logMessage("Device is connected via Wifi.");
connectionString = ";interface=wifi";
}
// Is the carrier network the only way to connect?
else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
{
logMessage("Carrier coverage.");
String carrierUid = getCarrierBIBSUid();
if(carrierUid == null)
{
// Has carrier coverage, but not BIBS. So use the carrier's TCP network
logMessage("No Uid");
connectionString = ";deviceside=true";
}
else
{
// otherwise, use the Uid to construct a valid carrier BIBS request
logMessage("uid is: " + carrierUid);
connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
}
}
// Check for an MDS connection instead (BlackBerry Enterprise Server)
else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
{
logMessage("MDS coverage found");
connectionString = ";deviceside=false";
}
// If there is no connection available abort to avoid bugging the user unnecssarily.
else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
{
logMessage("There is no available connection.");
}
// In theory, all bases are covered so this shouldn't be reachable.
else
{
logMessage("no other options found, assuming device.");
connectionString = ";deviceside=true";
}
return connectionString;
}
/**
* Looks through the phone's service book for a carrier provided BIBS network
* @return The uid used to connect to that network.
*/
private static String getCarrierBIBSUid()
{
ServiceRecord[] records = ServiceBook.getSB().getRecords();
int currentRecord;
for(currentRecord = 0; currentRecord < records.length; currentRecord++) { if(records[currentRecord].getCid().toLowerCase().equals("ippp")) { if(records[currentRecord].getName().toLowerCase().indexOf("bibs") >= 0)
{
return records[currentRecord].getUid();
}
}
}
return null;
}
这篇关于启用多个连接类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文