2013-09-30 58 views
1

我正在從服務器下載文件。我在下載時顯示進度條。當我試圖下載文件它顯示進度條。它在酒吧中顯示0,但我可以下載文件。我用下面的代碼進度條在下載時無響應

// onPreExecute

   protected void onPreExecute() { 

       super.onPreExecute(); 
        showDialog(progress_bar_type); 
       } 

給出//這是一個問的任務fordownloading

protected String doInBackground(String... arg0) { 
         int bufferLength = 0; 
        try { 
              //geting connection 
         URL url = new URL( weburl); 
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
         urlConnection.setRequestMethod("GET"); 
         urlConnection.setDoOutput(true); 
         urlConnection.connect(); 
        // getting file length 
         int lenghtOfFile = urlConnection.getContentLength(); 

         File sdcard = Environment.getExternalStorageDirectory(); 
         File file = new File(sdcard, "filename.extension"); 

         FileOutputStream fileOutput = new FileOutputStream(file); 
         InputStream inputStream = urlConnection.getInputStream(); 

         byte[] buffer = new byte[1024]; 

         long total = 0; 
         while ((bufferLength = inputStream.read(buffer))!= -1) { 
          total += bufferLength ; 
               //progress 
          publishProgress(""+(int)((total*100)/lenghtOfFile)); 
          fileOutput.write(buffer, 0, bufferLength); 
         } 

我使用onProgressUpdate來顯示結果,但沒有用。它仍然是0只

protected void onProgressUpdate(String...values) { 
         // setting progress percentage 
         pDialog.setProgress(Integer.parseInt(values[0])); 
        } 

的AsyncTask的// onPostExecute

    protected void onPostExecute(final Boolean success) { 

         dismissDialog(progress_bar_type);} 
+0

什麼是你的進度條最大值?默認情況下,它是10000,並將進展值在0..100的範圍內肯定會保持進度接近於0. – laalto

+0

@laalto 我使用的最大值爲100,還有一件事我總是將thelenghtOfFile設置爲-1 – DKV

+0

好。下一個問題:你是如何執行asynctask的? – laalto

回答

1

使用onProgressUpdate()方法來更新進度條。

參考link

+0

我已經使用,但沒有使用保護void onProgressUpdate(String ... progress){//設置進度百分比pDialog.setProgress(Integer.parseInt(progress [0])); } – DKV

+0

你在doInBackground()方法中使用publishProgress()方法嗎? – Swetank

+0

是的,我正在做publishProgress()在背後做。這不好嗎? – DKV

1

您需要在的AsyncTask

@Override 
protected void onProgressUpdate(String... values) { 
    // TODO Auto-generated method stub 
    super.onProgressUpdate(values); 
    inDeterminatePB.setProgress(Integer.parseInt((values[0]))); 
} 
+0

我已經在進步更新,但沒有用 保護無效onProgressUpdate(字符串...進度){ \t \t \t \t \t \t \t //設置進度百分比 \t \t \t \t pDialog.setProgress(整數使用。parseInt函數(進展[0])); \t \t \t \t} – DKV

3
long total = 0; 
int iterationCount = 0; 
int progress = 0; 
while ((bufferLength = inputStream.read(buffer))!= -1) { 
total += bufferLength ; 
progress = (int)((total*100)/lenghtOfFile) 
if(iterationCount >= 14 || progress == 100) { 
    iterationCount = 0; 
    publishProgress(String.valueOf(progress)); 
} else { 
    iterationCount += 1; 
} 

fileOutput.write(buffer, 0, bufferLength); 
} 

增加一個覆蓋方法根據你得到的文件相等長度的問題爲-1

這信息並不總是可用的。通常你會知道你正在下載的文件的長度。根據網絡服務器,協議,連接和下載方法的不同,這些信息可能並不總是可用的。

你一定要修改你的應用程序,以便它可以處理這種情況。我想你會發現使用不同連接方法的不同設備將提供不同的結果。

+0

我試過這段代碼,但不幸的是它也給出了相同的結果。仍然酒吧只顯示0,而下載進展良好 – DKV

+0

請編輯您的問題,並添加onPre和onPost功能 – Basbous

+0

我得到了我的錯誤, 總是lenghtOfFile保持爲-1.Why我是那樣的@babous? – DKV