恢复xml文件的长度
我需要恢复 xml 文件的长度。我已经更改了代码,请,我需要帮助。真的很烦人。
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL("http://sociable.co/wp-content/uploads/2011/04/android-wallpaper.png");
URLConnection urlConnection = sourceUrl.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
Log.v("the size of this file is : ", ""+file_size);
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
但是当我使用log.v
时,logCat中什么也没有出现。我该如何解决这个问题?
I need to recover the length of a xml file. I have changed the code , please , I need help. It's really annoying.
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL("http://sociable.co/wp-content/uploads/2011/04/android-wallpaper.png");
URLConnection urlConnection = sourceUrl.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
Log.v("the size of this file is : ", ""+file_size);
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
But when I use log.v
, nothing is appeared in the logCat. How can I resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这一行是不正确的:
((File) xr)
...如果你这样做:I think this line is incorrect:
((File) xr)
... what if you do this:我认为问题是您正在尝试解析 png 文件。
edit
:查看您的文件是否正确下载I think the problem is that you are trying to parse a png file.
edit
: See if your file gets downloaded properly您不能将 XMLReader 转换为文件。你会得到一个 ClassCastException。您应该在日志中看到这一点。 Christian 是对的(除了该方法是
openConnection()
而不是connect()
)。替换
为
我刚刚尝试过并且有效。
You cannot cast an XMLReader to a File. You get a ClassCastException. You should see that in your log. Christian is right (except that the method is
openConnection()
and notconnect()
).Replace
with
I have just tried it and it works.
也许试试这个:
Maybe try this: