【问题标题】:Problems with org.apache.harmony.xml.ExpatParser$ParseExceptionorg.apache.harmony.xml.ExpatParser$ParseException 的问题
【发布时间】:2011-11-02 12:50:48
【问题描述】:

我的 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);
        }   
    }
}

任何建议可能是什么问题?

【问题讨论】:

  • 感谢 Reno 的评论,我已阅读此答案,但无济于事。我没有使用 POST,如果解析器得到它的数据,它可以成功解析它们。

标签: android inputstream saxparser


【解决方案1】:

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

更多信息在这里:HttpClient 和这里:HttpURLConnection responsecode is randomly -1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 2017-05-11
    相关资源
    最近更新 更多