2011-04-27 45 views
0

我有一個網址,例如http://somedomain.com/sync_login/go123/go,並給出了一個XML,如果你的網絡瀏覽器(我使用Firefox)要去查看,輸出是這樣的:android:是否可以將文件下載到磁盤給定一個XML(從一個URL)?

<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,非常感謝提前!

回答

1
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.permission.INTERNET對允許在你的Android-manifest.xml中。我犯了這個愚蠢的錯誤僕人時間:)

+0

感謝您的回覆,但在什麼目錄下我會看到下載的文件? – Kris 2011-04-27 08:51:06

+0

@migz此方法返回下載文件的Uri,如果出現錯誤,則返回null。 – 2011-04-27 08:57:08

相關問題