Blackberry InputStream过早关闭

时间:2011-08-12 21:38:12

标签: blackberry

以下代码用于从Web服务器获取XML文件,而今天,在最后几次运行中,这会引发异常,并显示错误消息“stream close”。我从昨天起就没有修改过这段代码,也没有修改任何处理解析的方法。

这个想法是从fullurl中提取的XML文件中构建一个项目列表。列表中应该有20个项目(基于我现在使用的XML文件)。在最后几次运行中,解析操作抛出了上面提到的异常,并且只存储了5个项目。方法public void endDocument()永远不会被调用。

任何想法都会有所帮助,因为这必须转移到后台任务,我想在此之前解决。

public void getAndParseXML() {
        HttpConnection xmlcon = null;
        InputStream xmlinput     = null;
        SAXParserFactory spf  = null;

        String fullurl = this.getNewsUrl() + NewsListBuilderTask.CONNECTION_STRING; // URL of XML file along specification for connection type 

        if ( (TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_WIFI)) && (TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_WIFI)) )
            fullurl += NewsListBuilderTask.WIFI_STRING;

        try {
            xmlcon = (HttpConnection)Connector.open( fullurl, Connector.READ, false );  // open connection to XML source
            spf = SAXParserFactory.newInstance();                               // set up xml parsers
            xmlinput = xmlcon.openInputStream();                                // set up input stream

            SAXParser saxparser = spf.newSAXParser();   // create a new parser object
            saxparser.parse( xmlinput, 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
            this.createDefaultItem();
        }
        catch (SAXException ex) {
            System.out.println( "SAXException Caught:\t" + ex.getMessage() );
            ex.printStackTrace();
            this.createDefaultItem();
        }
        catch ( IllegalArgumentException ex ) {
            System.out.println( "IllegalArgumentException Caught:\t" + ex.getMessage() );
            ex.printStackTrace();
            this.createDefaultItem();
        }
        catch (ParserConfigurationException ex) {
            System.out.println( "ParserConfigurationException Caught:\t" + ex.getMessage() );
            ex.printStackTrace();
            this.createDefaultItem();
        }
        finally {

            if ( xmlinput != null) {
                try {
                    xmlinput.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();
                }
            }
        }
    }

注意:如果可用,fullurl结尾使用"http://somexmlfile.com?type=photo;deviceside=true"并附加";interface=wifi"

0 个答案:

没有答案