恢复xml文件的长度

发布于 2024-12-11 09:20:32 字数 1010 浏览 0 评论 0原文

我需要恢复 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

英雄似剑 2024-12-18 09:20:32

我认为这一行是不正确的: ((File) xr)...如果你这样做:

sourceUrl.connect();
long length = sourceUrl.getContentLength();

I think this line is incorrect: ((File) xr)... what if you do this:

sourceUrl.connect();
long length = sourceUrl.getContentLength();
想念有你 2024-12-18 09:20:32

我认为问题是您正在尝试解析 png 文件。

edit:查看您的文件是否正确下载

   void downloadXMLFile(String fileUrl){
              URL myFileUrl = null;
              try {

               myFileUrl= new URL(fileUrl);
              } catch (MalformedURLException e) {

               // TODO Auto-generated catch block
               e.printStackTrace();
          }

          try {

               HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
               conn.setDoInput(true);
               conn.connect();
               int length = conn.getContentLength();
               InputStream is = conn.getInputStream();
          } catch (IOException e) {

               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }
}

I think the problem is that you are trying to parse a png file.

edit: See if your file gets downloaded properly

   void downloadXMLFile(String fileUrl){
              URL myFileUrl = null;
              try {

               myFileUrl= new URL(fileUrl);
              } catch (MalformedURLException e) {

               // TODO Auto-generated catch block
               e.printStackTrace();
          }

          try {

               HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
               conn.setDoInput(true);
               conn.connect();
               int length = conn.getContentLength();
               InputStream is = conn.getInputStream();
          } catch (IOException e) {

               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }
}
绮筵 2024-12-18 09:20:32

您不能将 XMLReader 转换为文件。你会得到一个 ClassCastException。您应该在日志中看到这一点。 Christian 是对的(除了该方法是 openConnection() 而不是 connect())。

替换

long filesize = ((File) xr).length();

int filesize = sourceUrl.openConnection().getContentLength();

我刚刚尝试过并且有效。

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 not connect()).

Replace

long filesize = ((File) xr).length();

with

int filesize = sourceUrl.openConnection().getContentLength();

I have just tried it and it works.

梦明 2024-12-18 09:20:32

也许试试这个:

long contentLength = Long.parseLong(httpconn.getHeaderField("Content-Length"));

Maybe try this:

long contentLength = Long.parseLong(httpconn.getHeaderField("Content-Length"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文