2011-04-23 82 views

回答

0

使用HttpClient的。然後,用你的InputStream,檢查此鏈接:

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

,並將其寫入你想要的文件。

一個快速和骯髒的例子:

// This goes inside a try... 
HttpClient htc = new DefaultHttpClient(); 
HttpGet get = new HttpGet("http://www.google.com"); 
HttpResponse htr = null; 
htr = htc.execute(get); 
InputStream is = htr.getEntity().getContent(); 
File saveFile = new File(getApplicationContext().getFilesDir().getPath() + File.separatorChar + "datos.txt"); 
FileOutputStream fos = new FileOutputStream(saveFile); 
// Validate if exists and so on... 
int c; 
while ((c = is.read()) != -1) 
{ 
    fos.write(c); 
} 
// Catch, finally, close, etc... 

您可能還需要緩衝的讀取和寫入的代碼。

另一種方法就是使用:

InputStream is = URL("http://www.google.com").getContent(); 

這是給你的。我喜歡HttpClient,因爲你可以處理更多的事情。

+0

@VicentePlata你可以給我一個使用內部存儲的例子嗎?我認爲最好的一個我必須使用sharedpreference,但我不知道如何使用它:(我試圖把它放在我的httpconnection類中,但它給了我錯誤 – Livi 2011-04-23 03:58:17

+0

完成請看我編輯的答案 – 2011-04-23 04:48:48

+0

@ VicentePlata我嘗試了你的答案,我試着首先用烤麪包片顯示細節Toast.makeText(getApplicationContext(), c,Toast.LENGTH_SHORT).show();,但沒有反應:$ – Livi 2011-04-23 06:48:49

相關問題