2011-05-05 69 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