我的 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);
}
}
}
有什么建议可能是什么问题吗?
我找到了解决方案。问题不在于 XML 解析器,而在于 NSURLConnection 的错误实现。我切换到 HttpClient,问题就消失了。
更多信息在这里:Http客户端和这里:HttpURLConnection响应代码随机为-1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)