2011-05-05 71 views
1

我正在編寫一個應用程序,許多點都會嘗試從網站檢索帳戶信息。我想編寫一個函數(「getAccount()」)執行以下操作:顯示進度對話框,檢索數據並等待它

  1. 顯示ProgressDialog
  2. 進行調用的網站
  3. 等待響應
  4. 清除ProgressDialog
  5. 控制返回給調用函數的前四個步驟後完成

我並沒有用剛開的一個問題g來自頁面的數據;我遇到的問題是整個「顯示對話框/等待完成/返回控制到調用函數」部分。 ProgressDialog根本不顯示,或者該函數在從站點發出數據請求後立即返回給調用者,而沒有給它足夠的時間來檢索數據。

任何幫助將不勝感激。

編輯:我在下面添加了一些代碼,用於AsyncTask。請注意,我在grabURL()中有MsgBox("done")行;這只是一個Toast調用。當我運行這個代碼時,在完成HTTP請求時彈出「完成」。此MsgBox行只存在,所以我可以看到如果grabURL正在等待GrabURL完成(它不是)。

public void grabURL() { 
    new GrabURL().execute(); 
    MsgBox("done"); 
} 

private class GrabURL extends AsyncTask<String, Void, Void> { 
    private ProgressDialog Dialog = new ProgressDialog(MyContext); 

    protected void onPreExecute() { 
     Dialog.setTitle("Retrieving Account"); 
     Dialog.setMessage("We're retrieving your account information. Please wait..."); 
     Dialog.show(); 
    } 

    protected Void doInBackground(String... urls) { 
     try { 
      // Get account info from the website 
      String resp = GetPage(ThePage); // I have this classed out elsewhere 
      // Some other code that massages the data 
      AccountRetrievalSuccess = true; 
     } catch (Exception e) { 
      AccountRetrievalSuccess = false; 
     } 

     return null; 
    } 

    protected void onPostExecute(Void unused) { 
     Dialog.dismiss(); 
    } 

} 
當你想要做一個同步調用(這將阻止,並等待返回數據)到網站
+2

的AsyncTask,的AsyncTask,的AsyncTask – Klaus 2011-05-05 18:39:02

+0

你有沒有想出解決辦法?我想做同樣的事情,我很驚訝,我找不到一個簡單的解決方案。 http://stackoverflow.com/questions/15179517/show-progressdialog-during-a-network-call-until-its-finished – 2013-03-02 21:18:20

回答

0

不能肯定地說,沒有看到一些代碼,但聽起來好像你是一個異步調用的網站代替。

+0

這正是發生了什麼;不幸的是,我沒有能夠在不調用調用的情況下執行ProgressDialog;如果我在單個線程中運行它,則ProgressDialog不顯示。 – 2011-05-05 19:52:11

0

您想要使用AsyncTask,在onPreExecute中生成非用戶可取消的ProgressDialog,在doInBackground中執行您的工作,並在onPostExecute中將其解除。

public class MyApp extends Activity 
{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // blah blah blah 

    URL url; 
    try 
    { 
     url = new URL("http://example.com"); 
     new MyTask().execute(url); 
    } 
    catch (MalformedURLException e) 
    { 
    } 

    } 

    protected void doSomeStuff() 
    { 
    // stuff to do after the asynctask is done 
    } 


    protected void throwAWobbly() 
    { 
    // stuff to do if you didn't get the data 
    } 

    // inner class to do the data getting off the UI thread, 
    // so android doesn't "not responding" kill you 
    private class MyTask extends AsyncTask<URL, Void, Boolean> 
    { 
    private ProgressDialog dialog; 
    private boolean gotData = false; 

    protected void onPreExecute() 
    { 
     // create a progress dialog 
     dialog = ProgressDialog.show(MyApp.this, "", 
       "Doing stuff. Please wait...", false, false); 
    } 

    protected Boolean doInBackground(URL... urls) 
    { 
     // get your data in here! 
     return gotData; 
    } 

    protected void onPostExecute(Boolean result) 
    { 
     // get rid of the progress dialog 
     dialog.dismiss(); 

     if (true == result) 
     { 
     // got all data! 
     doSomeStuff(); 
     } 
     else 
     { 
     // oops! 
     throwAWobbly(); 
     } 
    } 
    } 

} 
+1

我試過了,很少或沒有成功。用上面的代碼查看我的編輯。 – 2011-05-05 19:14:48

1

,因爲的AsyncTask是使用一個單獨的線程(S)來運行doInBackground出現完成的消息框:

這樣的事情。執行調用不會阻止。您可以將消息框移至onPostExecute,然後再打電話關閉。小費。您可能想要在onPause中調用progress.cancel,否則您可能會在方向更改時收到不想要的行爲。最後,如果您在doInBackground中檢索信息,請考慮返回doInBackground中的信息。信息將被傳遞給onPostExecute。因此,如果信息是Object MyInfo的考慮:

private class GrabURL extends AsyncTask<String, Void, MyInfo> { 
+0

我試圖完成的是調用函數('grabURL')等到被調用的函數('GrabURL')完成後才返回 - 這就是爲什麼我有MsgBox的地方。我使用AsyncTask的唯一真正原因是ProgressDialog會正確顯示。 – 2011-05-05 19:36:27

+0

@Don:grabURL()不會等待,因爲它在顯示對話框的UI線程上運行(並且理論上已準備好管理任何輸入事件)。你將不得不依靠onPostExecute()來知道任務何時完成。 – bigstones 2011-05-05 20:04:06

+0

@Don Quixote不幸的是,時間密集的阻塞呼叫在Android中表現不佳。你會得到什麼是可怕的ANR沒有響應對話框。您可以使用asynchTask獲得相同的有效行爲,並像其他人提到的那樣發佈不可取消的進度對話框。這避免了ANR對話框。 – JAL 2011-05-05 20:04:57