2010-01-20 90 views
23

我想知道一些下載的進度。
當我收到從網上的一些資源, 這樣的:Android下載進度

 String myFeed = request.getURI().toString(); 
     URL url = new URL(myFeed); 
     URLConnection connection = url.openConnection(); 
     HttpURLConnection httpConnection = (HttpURLConnection) connection; 
     int responseCode = httpConnection.getResponseCode(); 
     if (responseCode == HttpURLConnection.HTTP_OK) { 

      InputStream in = httpConnection.getInputStream(); 
      // Process the input stream 
     } 

當 「的InputStream在= httpConnection.getInputStream();」被稱爲,
該文件的所有內容一次下載。

如果我使用:

 htttpResponse = httpClient.execute(httpRequestBase); 

的問題是一樣的。

如何檢測下載的實際進度?

謝謝大家!

回答

9

我覺得這已經是answered here

簡而言之:使用應該在標題字段中的「Content-Length」。

編輯:對不起,我沒有完全回答這個問題。基本上,一旦你知道預期的總體內容長度,那麼你可以跟蹤你收到的數據量並計算完成的百分比。

參見:How to create a download manager

+1

我已經知道文件的大小,通過getContentLength()。 我不知道如何獲得已收到內容的進度。我只談論一個文件。 – Tsimmi 2010-01-20 11:41:55

+0

哦,我明白了。好的,請參閱「如何創建下載管理器」鏈接。這有幫助嗎? – GaZ 2010-01-20 12:06:08

+0

對不起!起初我沒有看到這個鏈接。 – Tsimmi 2010-01-20 12:44:26