0

大家好,感謝您的閱讀。 :)如何在等待HttpRespons時顯示ProgressDialog?

我有這個Java(Android)代碼正在發出HTTP請求並等待響應。該請求啓動一個生成PDF文件並返回的服務。

該服務大約需要20秒,當用戶等待時,我想顯示進度對話框(不確定)。我試圖在它自己的線程中顯示對話框,這給我的運行時異常。我試着把請求和響應在自己的線程中,但沒有等待負責完成,我得到一個空的PDF。

任何人都可以提出任何建議嗎?這裏的代碼...

textContainer.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View view) { 

    Intent getPdfFile = null; 
    File pdfFile = new File(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf"); 

    if(!pdfFile.exists()) { 

     try { 

     DefaultHttpClient client = new DefaultHttpClient(); 
     HttpGet getMethod = new HttpGet((
         "http://myServices.mySite.org/services.ashx/PDFFILE?fileId=" 
         + fileId + "&account=" + accountId + "&dataset=" +                        
         account.getProperty("DATASET").getValue().toString().trim()).replace(" ", "%20")); 
     HttpResponse response = client.execute(getMethod); 

     InputStream inStream = response.getEntity().getContent(); 

     FileOutputStream fileWriter = new FileOutputStream(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf"); 

     int dataByte = inStream.read(); 
     while(dataByte > -1) { 
      fileWriter.write(dataByte); 
      dataByte = inStream.read(); 
     } 
     fileWriter.close(); 
     } 
     catch(Exception e) { 

     // TODO: Handle the exceptions... 
     } 

     getPdfIntent = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(pdfFile), "application/pdf");          
     startActivity(getPdfIntent); 
    } 
}); 

非常感謝提前! :)

編輯:這裏有一個例子,我已經使用AsyncTask來嘗試解決問題。

textContainer.setOnClickListener(new View.OnClickListener() { 

     public void onClick(final View view) { 

     Intent getPdfFile = null; 
     File pdfFile = new File(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf"); 

     if(!pdfFile.exists()) { 

      new AsyncTask<Boolean, Boolean, Boolean>() { 

       private ProgressDialog dialog; 

       protected void onPreExecute() { 

        dialog = ProgressDialog.show(view.getContext(), "Loading", "Please wait..."); 
       } 

       protected Boolean doInBackground(Boolean... unused) { 

        try { 

         DefaultHttpClient client = new DefaultHttpClient(); 
         HttpGet getMethod = new HttpGet((
            "http://myServices.mySite.org/services.ashx/PDFFILE?fileId=" 
            + fileId + "&account=" + accountId + "&dataset=" +                         
            account.getProperty("DATASET").getValue().toString().trim()).replace(" ", "%20")); 
         HttpResponse response = client.execute(getMethod); 

         InputStream inStream = response.getEntity().getContent(); 

         FileOutputStream fileWriter = new FileOutputStream(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf"); 

         int dataByte = inStream.read(); 
         while(dataByte > -1) { 
          fileWriter.write(dataByte); 
          dataByte = inStream.read(); 
         } 
         fileWriter.close(); 
        } 
        catch(Exception e) { 

         // TODO: Handle the exceptions... 
        } 
        return true; 
       } 

       protected void onPostExecute(Boolean unused) { 

        dialog.dismiss(); 
       } 
      }.execute(0); 

      getPdfIntent = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(pdfFile), "application/pdf");         
      startActivity(getPdfIntent); 
     } 
    }); 

但是,會發生什麼是我不等待HttpRequest的響應,並繼續作爲如果迴應immidiately返回。 : -/

+0

您正在ui線程上進行網絡調用。你不應該那樣做。改爲使用異步任務。 – njzk2 2012-03-07 09:38:00

+0

njzk2是正確的,我認爲它實際上是不允許在Android 3.0 SDK和以上 – Tony 2012-03-07 09:40:30

+0

剛剛編輯我的帖子,包括我試過的AsyncTask,沒有任何運氣。 – 2012-03-07 14:37:09

回答

2

它看起來像你需要把你的最後兩行內else {}塊,也在onPostExecute中,以便它只在文件存在或AsyncTask完成後才執行。

+0

我愛你們,非常感謝!這固定了一切。我不知道爲什麼我自己沒有想到,但非常感謝! :) – 2012-03-09 09:37:35

0

您將需要使用AsyncTask ...本教程應該爲您提供基礎知識。

http://droidapp.co.uk/?p=177

+0

+1是的同意此鏈接。檢查鏈接中給出的代碼。 – 2012-03-07 09:46:22

+0

以下是我所做的...在onPreExecute方法中顯示對話框,在doInBackground方法中發出請求並在onPostExecute方法中關閉對話框。會發生什麼是它在響應完成之前繼續嘗試並顯示PDF文件。換句話說,它不會等待響應結束。當我收到錯誤消息,指出文件無法讀取並按回時顯示永遠不會被解除的對話框? : - | – 2012-03-07 10:34:49

+0

你可以修改你的第一篇文章,並把你正在使用的新代碼? – Tony 2012-03-07 10:54:40

0

ü應該使用的AsyncTask做到這一點。在覆蓋方法「doInBackground」你可以發佈你的httppost和showdialog覆蓋方法「onPreExecute」,然後取消它onPostExecute

+0

這正是我已經嘗試過,但它不會等待響應,它只是繼續。 – 2012-03-07 10:00:28

+0

我不明白,你的意思是你只做一個HTTP請求,那麼你就不會在乎響應。但你的問題是如何在等待迴應時顯示progressdialog ...什麼都可以。我想也許你可以使用hanlder來做到這一點。 – sam 2012-03-22 06:29:32