问题
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