首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android:给出一个xml (从URL),是否可以将文件下载到磁盘?

android:给出一个xml (从URL),是否可以将文件下载到磁盘?
EN

Stack Overflow用户
提问于 2011-04-27 10:22:10
回答 1查看 720关注 0票数 0

我有一个网址,例如http://somedomain.com/sync_login/go123/go,它给出了一个XML,如果你打算在web浏览器(我使用火狐)中查看它,输出是这样的:

代码语言:javascript
复制
<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>

我想将该XML上提供的信息和文件下载到我的平板电脑上。

我是android dev的新手,非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-27 10:44:59

代码语言:javascript
复制
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;
  }

我在我的AsyncTask类中编写了这个方法,所以我使用publishProgress来更新进度,您可以删除该行。但我建议你也写你的AsyncTask。

希望它能有所帮助:)

别忘了在android-manifest.xml中添加android.permission.INTERNET权限。我犯了几次这个愚蠢的错误:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5798651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档