简体   繁体   English

停止SAX分析器后,为什么InputStream会继续下载文件?

[英]Why does my InputStream keep downloading file after I stop the SAX Parser?

I'm working on an RSS feed parser in Android and I've implemented a SAX parser which works perfectly under most situations. 我正在研究Android中的RSS feed解析器,并且已经实现了SAX解析器,该解析器在大多数情况下都可以完美运行。

However, I've run into problems on a few of my test feeds. 但是,我在一些测试提要中遇到了问题。 After I've parsed a specified number of feed items, I throw a SAXException to stop the parser, which AFAIK is the right way to do that. 在解析了指定数量的Feed项目之后,我抛出了SAXException来停止解析器,而AFAIK是正确的方法。 On most feeds, this stops the parsing and my catch block (see below) handles and logs the StopParsingException. 在大多数提要中,这将停止解析,并且我的catch块(请参见下文)将处理并记录StopParsingException。

On SOME feeds, however, the parser stops parsing, but there is a long delay between the exception being thrown and my catch block being run, during which no parsing is done, but just enough time passes to download the entire file (which is what I suspect is happening). 但是,在某些feed上,解析器停止解析,但是在引发异常和运行我的catch块之间有很长的延迟,在此期间不进行解析,但是只有足够的时间来下载整个文件(这就是我怀疑正在发生)。

Here's my setup and error handling code: 这是我的设置和错误处理代码:

public boolean parse(){
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            SAXParser parser = factory.newSAXParser();
            URL u = new URL(mUrl);
            URLConnection UC = u.openConnection();
            UC.setConnectTimeout(CONNECT_TIMEOUT);
            UC.setReadTimeout(CONNECT_TIMEOUT);
            InputStreamReader r = new InputStreamReader(UC.getInputStream());
            parser.parse(new InputSource(r), this);     
        }catch(SAXException sax)
        {
            Exception ex = sax.getException();
            if(ex != null)
            {
                if(ex instanceof StopParsingException)
                {
                    //Feed was intentionally stopped (i.e. reached episode limit)
                    DebugLog.w(TAG, "Feed update stopped for: " + mUrl, ex);
                    return true;
                }else
                {
                    //Something went wrong, non-standard error
                    DebugLog.e(TAG, "Feed update failed for: " + mUrl, ex);
                    return false;
                }
            }else{
                //Something went wrong, non-standard error
                DebugLog.e(TAG, "Feed update failed fatally for: " + mUrl, sax);
                return false;
            }

        }
        catch(Exception e){
            DebugLog.e(TAG, "Unknown parse error on feed: "+mUrl, e);
            return false;
        }
        DebugLog.i(TAG, "Entire Feed Parsed successfully: "+mUrl);
        return true;
    }

When one of my conditions is met, I use this code: 当满足我的条件之一时,我将使用以下代码:

throw (new SAXException(new StopParsingException("Max Items reached")));

for example to stop the parser. 例如停止解析器。

My guess is that when I throw the exception, the SAXParser stops working, but the InputSteamReader continues to download the rss feed from the server, since that is almost exactly the timing that my logs reveal. 我的猜测是,当我引发异常时,SAXParser停止工作,但是InputSteamReader继续从服务器下载rss feed,因为这几乎正是我的日志揭示的时间。

Is there something wrong with my connection setup that makes only some servers not cooperate with me? 我的连接设置是否有问题,导致仅某些服务器无法与我合作?

Alternatively is there a way to directly stop that InputStream safely before throwing my SAXException so that I don't have this problem? 或者,有没有一种方法可以在抛出SAXException之前直接安全地停止InputStream,这样我就不会遇到这个问题?

It is just possible that the SAX parser is trying to "recover" in some way and reading your input stream to (for example) close matching tags. SAX解析器很可能试图以某种方式“恢复”并将您的输入流读取到(例如)关闭匹配的标签。 If this was occurring, you could probably prevent it by closing the input stream before throwing your exception. 如果发生这种情况,您可以通过在引发异常之前关闭输入流来防止此情况。

Another alternative would be to register an error handler . 另一种选择是注册一个错误处理程序 In fact, the javadoc suggests that this could be the more correct approach. 实际上,javadoc建议这可能是更正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM