机器人:是否有可能下载中心文件保存到磁盘给出一个xml(从URL)? [英] android: Is it possible to dowload file to disk given an xml (from a URL)?

查看:58
本文介绍了机器人:是否有可能下载中心文件保存到磁盘给出一个xml(从URL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个URL,例如 http://somedomain.com/sync_login/go123/go,它给了一个XML,如果你要查看其网络浏览器(我使用Firefox),输出是这样的:

 < SyncLoginResponse>
    <影片>
    <视频>
        <名称> 27 / FLV< /名称>
        <冠军>摩托车< /标题>
        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/videos/< / URL&GT;
        <thumbnail>http://somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/106/jpg</thumbnail>

    &LT; /视频&GT;
    &LT; /视频&GT;
    &LT;幻灯片&GT;
    &LT;幻灯片&GT;
        &LT;名称&gt; 44 LT; /名称&gt;
        &LT;冠军&GT; ProcessFlow&LT; /标题&GT;
        &LT;网页&GT; 4℃/页&GT;

        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/slideshows/< / URL&GT;
    &LT; /幻灯片&GT;
    &LT;幻灯片&GT;
        &LT;名称&gt; 71 LT; /名称&gt;
        &LT;冠军&GT; Processflows&LT; /标题&GT;
        &LT;网页&GT;第3版; /页&GT;
        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/slideshows/< / URL&GT;

    &LT; /幻灯片&GT;
    &LT; /幻灯片&GT;
    &LT;表格&GT;
    &LT;表格&GT;
        &LT;名称&gt;最佳外形自从/ HTML&LT; /名称&gt;
        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/form/< / URL&GT;
    &LT; /表&GT;
    &LT; /表格&GT;

    &LT;网站&GT;
    &LT;网站&GT;
        &LT;名称&gt; Facebook和LT; /名称&gt;
        &LT;网址&GT; HTTP://www.facebook.com< / URL&GT;
    &LT; /网站&GT;
    &LT; /网站&GT;
    &LT;调查&GT;
    &LT;调查与GT;

        &LT;名称&gt; CokeSurvey / HTML&LT; /名称&gt;
        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/survey/< / URL&GT;
    &LT; /勘测GT;
    &LT; /调查&GT;
    &LT;活采&GT;
    &LT;互动&GT;
        &LT;名称&gt; 34 / SWF&LT; /名称&gt;

        &LT;冠军&GT; PirateGem&LT; /标题&GT;
        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/interactives/< / URL&GT;
        <thumbnail>http://somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/110/png</thumbnail>
    &LT; /互动&GT;
    &LT;互动&GT;
        &LT;名称&gt; 36 / SWF&LT; /名称&gt;
        &LT;冠军&GT;井字游戏&LT; /标题&GT;

        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/interactives/< / URL&GT;
        <thumbnail>http://somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/106/jpg</thumbnail>
    &LT; /互动&GT;
    &LT; /活采&GT;
    &LT;外观&GT;
    &LT;皮肤&GT;
        &LT;冠军&GT; CokeZero&LT; /标题&GT;

        &LT; fontcolor&GT; F50A0A&LT; / fontcolor&GT;
        &LT;的backgroundColor&GT; 787777&LT; /的backgroundColor&GT;
        &LT;复位&GT; 18复位/ PNG&LT; /复位&GT;
        &LT;幻灯片&GT; 18幻灯片/ PNG&LT; /幻灯片&GT;
        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/skins/< / URL&GT;
    &LT; /皮肤&GT;

    &LT; /皮肤&GT;
    &LT; AdminSites&GT;
    &LT; AdminSite&GT;
        &LT;名称&gt;雅虎和LT; /名称&gt;
        &LT;网址&GT; HTTP://www.yahoo.com< / URL&GT;
    &LT; / AdminSite&GT;
    &LT; / AdminSites&GT;
    &LT; AdminSlideshows&GT;

    &LT; AdminSlideshow&GT;
        &LT;名称&gt; 71 LT; /名称&gt;
        &LT;冠军&GT; Processflows&LT; /标题&GT;
        &LT;网页&GT;第3版; /页&GT;
        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/slideshows/< / URL&GT;
    &LT; / AdminSlideshow&GT;
    &LT; / AdminSlideshows&GT;

    &LT; AdminVideos&GT;
    &LT; AdminVideo&GT;
        &LT;名称&gt; 27 / FLV&LT; /名称&gt;
        &LT;冠军&GT;摩托车&LT; /标题&GT;
        &LT;网址&GT; HTTP://somedomain.com/tabletcms/tablets/tablet_content/000002/videos/< / URL&GT;
        <thumbnail>http://somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/106/jpg</thumbnail>
    &LT; / AdminVideo&GT;

    &LT; / AdminVideos&GT;
    &LT;的UserDetails&GT;
        &LT;用户id&GT; 137&LT; /用户id&GT;
        &LT; USERFIRSTNAME&GT; Jayem&LT; / USERFIRSTNAME&GT;
        &LT; USERLASTNAME&GT; Dalisay&LT; / USERLASTNAME&GT;
        &LT;邮件&gt;&LT; /邮件&gt;
        &LT;公司名称&GT;约翰·斯图尔特公司&LT; /公司名称&GT;

    &LT; /的UserDetails&GT;
&LT; / SyncLoginResponse&GT;
 

我想下载到我的平板电脑上的XML提供的资料和文件。

我是新的Andr​​oid开发人员,许多在此先感谢!

解决方案

 私人乌里downloadFileFromURL(URL网址,上下文的背景下,字符串文件名){
    尝试 {
      URLConnection的康恩= url.openConnection();
      HttpURLConnection类的HttpConnection =康涅狄格州的instanceof HttpURLConnection的? (HttpURLConnection类),康涅狄格州:空;
      如果(HttpConnection的!= NULL){
        INT CONTENTLENGTH = httpConnection.getContentLength();
        INT LEN,长度= 0;
        byte []的BUF =新的字节[8192];
        InputStream的是= httpConnection.getInputStream();
        档案文件=新的文件(context.getExternalFilesDir(空),文件名);
        的OutputStream OS =新的FileOutputStream(文件);
        尝试 {
          而((LEN = is.​​read(buf中,0,buf.length))大于0){
            os.write(BUF,0,的len);
            长+ = len个;
            publishProgress((INT)(PROGRESS_MAX *(浮点)长/ CONTENTLENGTH));
          }
          os.flush();
        }
        最后 {
          is.close();
          os.close();
        }
        返回Uri.fromFile(文件);
      }
    }
    赶上(IOException异常E)
    {
       //异常处理到这里
    }
    返回null;
  }
 

我在我的AsyncTask类写了这个方法,所以我用publishProgress更新进度,可以删除该行。但我建议你写你的AsyncTask的为好。

希望它可以帮助:)

不要忘记添加android.permission.INTERNET对许可使用Android的manifest.xml。我做了这个愚蠢的错误薮时间:)

I have a URL, for example http://somedomain.com/sync_login/go123/go and it gives an XML, if you're gonna view it in web browser (i use firefox), the output is something like this:

<SyncLoginResponse>
    <Videos>
    <Video>
        <name>27/flv</name>
        <title>scooter</title>
        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/videos/</url>
        <thumbnail>http://somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/106/jpg</thumbnail>

    </Video>
    </Videos>
    <Slideshows>
    <Slideshow>
        <name>44</name>
        <title>ProcessFlow</title>
        <pages>4</pages>

        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/slideshows/</url>
    </Slideshow>
    <Slideshow>
        <name>71</name>
        <title>Processflows</title>
        <pages>3</pages>
        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/slideshows/</url>

    </Slideshow>
    </Slideshows>
    <Forms>
    <Form>
        <name>Best Form Ever/html</name>
        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/form/</url>
    </Form>
    </Forms>

    <Websites>
    <Website>
        <name>facebook</name>
        <url>http://www.facebook.com</url>
    </Website>
    </Websites>
    <Surveys>
    <Survey>

        <name>CokeSurvey/html</name>
        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/survey/</url>
    </Survey>
    </Surveys>
    <Interactives>
    <Interactive>
        <name>34/swf</name>

        <title>PirateGem</title>
        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/interactives/</url>
        <thumbnail>http://somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/110/png</thumbnail>
    </Interactive>
    <Interactive>
        <name>36/swf</name>
        <title>tictactoe</title>

        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/interactives/</url>
        <thumbnail>http://somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/106/jpg</thumbnail>
    </Interactive>
    </Interactives>
    <Skins>
    <Skin>
        <title>CokeZero</title>

        <fontcolor>F50A0A</fontcolor>
        <backgroundcolor>787777</backgroundcolor>
        <reset>18-reset/png</reset>
        <slideshows>18-slideshows/png</slideshows>
        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/skins/</url>
    </Skin>

    </Skins>
    <AdminSites>
    <AdminSite>
        <name>yahoo</name>
        <url>http://www.yahoo.com</url>
    </AdminSite>
    </AdminSites>
    <AdminSlideshows>

    <AdminSlideshow>
        <name>71</name>
        <title>Processflows</title>
        <pages>3</pages>
        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/slideshows/</url>
    </AdminSlideshow>
    </AdminSlideshows>

    <AdminVideos>
    <AdminVideo>
        <name>27/flv</name>
        <title>scooter</title>
        <url>http://somedomain.com/tabletcms/tablets/tablet_content/000002/videos/</url>
        <thumbnail>http://somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/106/jpg</thumbnail>
    </AdminVideo>

    </AdminVideos>
    <UserDetails>
        <userid>137</userid>
        <userfirstname>Jayem</userfirstname>
        <userlastname>Dalisay</userlastname>
        <messages></messages>
        <companyname>John Stewart Company</companyname>

    </UserDetails>
</SyncLoginResponse>

I want to download to my tablet the information and files given on that XML.

I'm new to android dev, many thanks in advance!

解决方案

private Uri downloadFileFromURL(URL url, Context context, String fileName) {
    try {
      URLConnection conn = url.openConnection();
      HttpURLConnection httpConnection = conn instanceof HttpURLConnection ? (HttpURLConnection ) conn  : null;
      if(httpConnection != null) {
        int contentLength = httpConnection.getContentLength();
        int len, length = 0;
        byte[] buf = new byte[8192];
        InputStream is = httpConnection.getInputStream();
        File file = new File(context.getExternalFilesDir(null), fileName);
        OutputStream os = new FileOutputStream(file);
        try {
          while((len = is.read(buf, 0, buf.length)) > 0) {
            os.write(buf, 0, len);
            length += len;
            publishProgress((int) (PROGRESS_MAX * (float) length / contentLength));
          }
          os.flush();
        }
        finally {
          is.close();
          os.close();
        }
        return Uri.fromFile(file);
      }
    }
    catch(IOException e)
    {
       //Exception handling goes here
    }
    return null;
  }

I wrote this method in my AsyncTask class, so I use publishProgress to update progress, you can remove that line. But I suggest you wrote your AsyncTask as well.

Hope it helps :)

And dont forget to add android.permission.INTERNET permission in your android-manifest.xml. I made this stupid mistake serval times :)

这篇关于机器人:是否有可能下载中心文件保存到磁盘给出一个xml(从URL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆