2014-01-12 82 views
0

我正在編寫一些代碼以發現Android中處理異步的不同方式,而且我已經完成了AsyncTask,現在我已經有了過去的未來,但是這一次它讓我有點...在等待未來完成時未顯示進度條

代碼本身工作正常,除了(旋轉)進度條只在下載操作完成後才顯示的事實......儘管我告訴代碼之前顯示它...

下面的代碼:

public void startDownload(final String url) { 
    bar.setVisibility(View.VISIBLE); // progress bar should appear here 

    ExecutorService pool = Executors.newCachedThreadPool(); 
    Gson gson = new Gson(); 

    Future<String> promise = pool.submit(new Callable<String>() { 
     @Override 
     public String call() throws Exception { 
      String response = ""; 
      DefaultHttpClient client = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(url); 
      try { 
       HttpResponse execute = client.execute(httpGet); 
       InputStream content = execute.getEntity().getContent(); 

       BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 

       String s = ""; 

       while((s = buffer.readLine()) != null) response += s; 

      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } 
      return response; 
     } 
    }); 


    while(promise.isDone() == false) { 
     // wait 
    } 

    bar.setVisibility(View.GONE); // and disappear here 

    try { 
     GitHubStatus status = gson.fromJson(promise.get(), GitHubStatus.class); 

     statusText.setText(status.getStatus()); 
     bodyText.setText(status.getBody()); 
     dateText.setText(status.getCreationDate()); 
    } catch (ExecutionException ee) { 
     ee.printStackTrace(); 
    } catch (InterruptedException ie) { 
     ie.printStackTrace(); 
    } 

    // instead it briefly appears here, and then disappears again 
} 

對於那些不知道的人;期貨執行自己,因此你不需要調用「運行」或「開始」方法來啓動它

我試圖把一個Log.v("DEBUG: ", "Loading...");在等待它完成的循環中,它確實打印郵件就好,很多時候在那個

我沒有試過循環,那也不行,而且我甚至有ProgressDialog,不能正常工作,嘗試...

我知道我無法在UI線程忙時更新UI線程,但我只在UI線程中的任何工作完成之前和之後設置了可見性,所以這裏有什麼問題?爲什麼它不工作?

+0

請顯示您的佈局文件。 – arts777

+0

我的佈局文件在這裏是無關緊要的,我知道progessbar出現在它應該在的地方,它只是不應該出現,當它應該 –

+0

人們應該停止要求與被問的問題無關的東西。 – FWeigl

回答

0

嘗試以下操作:

public void startDownload(final String url) { 
    bar.setVisibility(View.VISIBLE); // progress bar should appear here 

    ExecutorService pool = Executors.newCachedThreadPool(); 
    Gson gson = new Gson(); 

    Future<String> promise = pool.submit(new Callable<String>() { 
     @Override 
     public String call() throws Exception { 
      String response = ""; 
      DefaultHttpClient client = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(url); 
      try { 
       HttpResponse execute = client.execute(httpGet); 
       InputStream content = execute.getEntity().getContent(); 

       BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 

       String s = ""; 

       while((s = buffer.readLine()) != null) response += s; 

      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } 

      runOnUiThread(new Runnable() 
      { 
       public void run() 
       { 
        bar.setVisibility(View.GONE); // and disappear here 

        try { 
         GitHubStatus status = gson.fromJson(promise.get(), GitHubStatus.class); 

         statusText.setText(status.getStatus()); 
         bodyText.setText(status.getBody()); 
         dateText.setText(status.getCreationDate()); 
        } catch (ExecutionException ee) { 
         ee.printStackTrace(); 
        } catch (InterruptedException ie) { 
         ie.printStackTrace(); 
        } 
       } 
      });  
      return response; 
     } 
    }); 
} 

由於

while(promise.isDone() == false) { 
      // wait 
     } 

你阻擋UIThread。

+0

至於他的代碼告訴我們他沒有使用AsyncTask。 – FWeigl

+0

但是不是未來應該在後臺做它的事嗎?在一個單獨的線程中,我的意思是......通過該定義,它不會阻塞UI線程......但相反,UI線程只是掛起,直到完成執行未來,即使沒有循環...爲什麼?不應該進度條變得可見嗎?我的意思是我在開始下載之前設置了可見性... –

+0

還附帶了您要顯示的代碼,您正試圖從內部訪問未來... –