Моя реализация 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);
}
}
}
Есть предложения, в чем может быть проблема?