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

时间:2011-11-02 12:50:48

标签: android inputstream saxparser

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

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

1 个答案:

答案 0 :(得分:1)

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

此处有更多信息:HttpClient和此处:HttpURLConnection responsecode is randomly -1