2011-05-06 92 views
0

我在按钮上使用AsyncTask单击刷新屏幕。以下为BTN发生的事件的顺序点击AsyncTask:使用网络服务和线程

  1. 进度对话框显示了
  2. 的doInBackground被称为和线程初始化它调用的Web服务。 Web服务提取/上传数据。一旦Web服务被调用,就会设置通过/失败标志。

我的问题是onPostExecute从不被调用,因此屏幕永远不会刷新。 其次,当数据下载并且Web服务设置标志时,我的代码已经在doInBackground中返回stmt。

问题是我如何停止执行我的asynctask,以便Web服务完成下载/上传数据并最终执行onPostexecute。

仅供参考 我也得到了以下警告从 日食

的方法onPostExecute(Boolean)将 型Screen.ConnectWebService是 从不在本地

private class ConnectWebService extends AsyncTask <Void, Void, Boolean> 
     { 
      private final ProgressDialog pd = new ProgressDialog(screen.this); 

      protected void onPreExecute() { 
       pd.show(Screen.this, "Sync", "Sync in progress",true,false); 
      } 


      protected Boolean doInBackground(Void... unused) { 

       if (SyncInProgress == false) 
       { 

        CallWSThread();//creates thread which calls web service 
       } 
         Log.d("doInBackground","doInBackground"); 
       return SyncStatus; 
      } 

      protected Void onPostExecute(boolean result) 
      { 

       pd.dismiss(); 
       if (result==true) drawRadioButtons(); 

       return null; 
      } 

     } 
+0

只要删除你的方法,让eclipse自动生成它们。它将使用正确的输入参数,修饰符和重写符号创建它们。 – 2011-05-06 18:52:50

回答

4

它应该使用be:

protected Void onPostExecute(Boolean result) 
+0

是的,这确实解决了问题..partly,onPostExecute()现在调用。但仍然不知道如何停止执行,直到所有的下载完成,然后调用onPostExecute() – jsp 2011-05-06 20:35:16

+0

你是否必须创建一个单独的线程来调用WS?你可以在doInBackground方法中同步访问它吗? – djg 2011-05-06 20:41:41

+0

试过这样做......但是当数据从服务器下载时,它已经移动到'onPostExecute()' – jsp 2011-05-06 22:07:02

1

正如djg所指出的,你的方法声明中有一个错字。当您从超类实现方法时,您可以通过使用注释@Override来避免这些类型的错误。