我正在尝试获取一个XML文件,以便使用以下内容进行解析:
private void getAndParseXML( String _xmlurl ) {
HttpConnection xmlcon = null;
InputStream input = null;
SAXParserFactory spf = null;
try {
xmlcon = (HttpConnection)Connector.open( _xmlurl, Connector.READ ); // open connection to XML source
spf = SAXParserFactory.newInstance(); // set up xml parsers
input = xmlcon.openInputStream(); // set up input stream
SAXParser saxparser = spf.newSAXParser(); // create a new parser object
saxparser.parse( input, this ); // parse operations start here
}
catch( IOException ex ) {
System.out.println( "IOException Caught:\t" + ex.getMessage() ); // set a default item if any exception occurs with retreiving or parsing XML file
}
catch (SAXException ex) {
System.out.println( "SAXException Caught:\t" + ex.getMessage() );
ex.printStackTrace();
}
catch ( IllegalArgumentException ex ) {
System.out.println( "IllegalArgumentException Caught:\t" + ex.getMessage() );
ex.printStackTrace();
}
catch (ParserConfigurationException ex) {
System.out.println( "ParserConfigurationException Caught:\t" + ex.getMessage() );
ex.printStackTrace();
}
finally {
if ( input != null) {
try {
input.close(); // attempt to close all connections
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if ( xmlcon != null ) {
try {
xmlcon.close();
}
catch ( IOException ex ) {
ex.printStackTrace();
}
}
}
} // END ----------------------------------------------------------------------------但是I会抛出一个异常,说明连接在12秒后超时。这是在执行行input = xmlcon.openInputStream();之后执行的。
如果这是相关的,则捕获IOException,并在调用此方法之前确定是否存在活动的网络连接。我错过了什么吗?
编辑:只是为了澄清,这将是应用程序中网络连接的第一个实例。在这段代码之前,先做一个简单的测试:
private boolean isConnectedToNetwork() {
boolean isConnected = false;
if ( (TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_CELLULAR)) || (TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_WIFI)) )
if ( (TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_CELLULAR)) || (TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_WIFI)) )
isConnected = true;
return isConnected;
}以确保连接是可能的,然后再尝试检索XML文件。
发布于 2011-08-12 04:37:49
找到问题所在。url (在本例中为_xmlurl )需要附加有";deviceside=true"以确保建立了直接的TCP/IP连接。这样可以确保通过蜂窝网络生成HttpConnection。换句话说,为了确保连接不是通过Blackberry MDS建立的。
此外,还需要检查:
if ( (TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_WIFI)) && (TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_WIFI)) )wi-fi天线是开着的。如果上面的值为true,则url (同样是_xmlurl)需要进一步附加";interface=wifi"以避开蜂窝网络,但仍然打开直接的TCP/IP连接。
发布于 2011-08-06 22:53:18
迈克,一切看起来都很好。
然而,这里有一些想法需要考虑:
https://stackoverflow.com/questions/6963715
复制相似问题