Problems with org.apache.harmony.xml.ExpatParser$ParseException

孤人 提交于 2019-12-02 09:08:10

问题


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?


回答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



来源:https://stackoverflow.com/questions/7980588/problems-with-org-apache-harmony-xml-expatparserparseexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!