org.apache.harmony.xml.ExpatParser$ParseException 的问题
我的 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);
}
}
}
有什么建议可能是什么问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案。问题不在于 XML 解析器,而在于 NSURLConnection 的错误实现。我切换到 HttpClient,问题就消失了。
更多信息在这里:HttpClient 和这里:HttpURLConnection响应代码是随机的-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