org.apache.harmony.xml.ExpatParser$ParseException 的问题

发布于 2024-12-13 02:05:26 字数 1628 浏览 2 评论 0原文

我的 SaxParser 实现有时会抛出

org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found

异常。在下一次尝试中,效果非常好。 一般来说,互联网连接没有问题。

这是我的实现。

1) 所有解析器的基类

public abstract class BaseFeedParser{

    private final URL url;
    private InputStream is;

    protected BaseFeedParser(String url) {
        try {
            this.url = new URL(url);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    protected InputStream getInputStream() {
        try {
            this.is = url.openConnection().getInputStream();
            return is;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected void closeInputStream() throws IOException{
        if(this.is!=null)
            this.is.close();
    }
}

2) 示例解析器

public class Parser extends BaseFeedParser {

    public void parse() {
        RootElement root = new RootElement("xml");
        //additional 
        Element child = root.getChild("child");
        child.setStartElementListener(new StartElementListener() {          
            @Override
            public void start(Attributes attributes) {
                // do something....
            }
        });

        try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root
                .getContentHandler());

            closeInputStream();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }   
    }
}

有什么建议可能是什么问题吗?

My SaxParser implementation throws sometimes a

org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found

Exception. At the next attempt it works perfectly good.
In general there is no problem with the internet connection.

Here is my implementation.

1) base class for all parser

public abstract class BaseFeedParser{

    private final URL url;
    private InputStream is;

    protected BaseFeedParser(String url) {
        try {
            this.url = new URL(url);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    protected InputStream getInputStream() {
        try {
            this.is = url.openConnection().getInputStream();
            return is;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected void closeInputStream() throws IOException{
        if(this.is!=null)
            this.is.close();
    }
}

2) a example parser

public class Parser extends BaseFeedParser {

    public void parse() {
        RootElement root = new RootElement("xml");
        //additional 
        Element child = root.getChild("child");
        child.setStartElementListener(new StartElementListener() {          
            @Override
            public void start(Attributes attributes) {
                // do something....
            }
        });

        try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root
                .getContentHandler());

            closeInputStream();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }   
    }
}

Any suggestions what might be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2024-12-20 02:05:26

我找到了解决方案。问题不在于 XML 解析器,而在于 NSURLConnection 的错误实现。我切换到 HttpClient,问题就消失了。

更多信息在这里:HttpClient 和这里:HttpURLConnection响应代码是随机的-1

I've found the solution. The problem was not the XML-Parser, but the buggy implementation of NSURLConnection. I switched to HttpClient and the problem vanished.

More infos here: HttpClient and here: HttpURLConnection responsecode is randomly -1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文