2012-04-16 57 views
0

我正在使用类扩展Asynctask来下载数据表单webservice。为了防止数据丢失和请求超时,我试图重复这个asynctask并每隔100行读取一次数据。但循环错误,从不运行任务:如何在完成后重复AsyncTask

功能重复任务:

public void executeCustomer(String vSpvId){ 
    String vURL; 
    int vloop = 1; 

    while(notStop){ 
     vURL = Routines.URL_CUSTOMER + "?vloop=" + String.ValueOf(vLoop) + "&vsupvid=" + vSpvId; 
     Log.d(TAG, vURL); 
     vCustomerAsyncTask = new CustomerAsyncTask(); 
     vCustomerAsyncTask.execute(vURL); 
    vloop++; 
    } 
} 

任务,下载数据:

private class CustomerAsyncTask extends AsyncTask<String, Void, String>{ 

    @Override 
    protected void onPreExecute() { 
     showProgressBar(Routines.WAIT); 
    } 

    @Override 
    protected String doInBackground(String... vURL) { 
     String vASPXText = Routines.getASPXText(vURL[0]); 
     return vASPXText; 
    } 

    @Override 
    protected void onPostExecute(String vASPXResult) { 
     super.onPostExecute(vASPXResult); 

     try{ 
      if((vASPXResult.length() == 0)||(vASPXResult.contains("masalah"))){ 
       vDialogBox.dismiss(); 
       Toast.makeText(getApplicationContext(), Routines.DATABASE_ERROR, Toast.LENGTH_SHORT).show(); 
       throw new NullPointerException(Routines.DATABASE_ERROR);     
      } 
     }catch(NullPointerException e){ 
      e.printStackTrace(); 
      Log.w(TAG, Routines.DATABASE_ERROR); 
      vDialogBox.dismiss(); 
      showDialogBox(Routines.WARNING, Routines.DATABASE_ERROR); 
      return; 
     } 
     vDialogBox.dismiss(); 

     notStop = parsingCustomer(vASPXResult); 
    } 
} 

函数来分析数据:

private boolean parsingCustomer(String vASPXResult){  
    try{ 
     Log.d(TAG, vASPXResult); 
     if(vASPXResult.indexOf("[email protected]`") == 0){ 
      // function to parse data 
      // return true to read next data from webservice 
      return = true; 
     }else if (vASPXResult.indexOf("[email protected]`") == 0){ 
      // function to parse data 
      // return false to stop read data from webservice 
      return = false; 
     } 
    }catch (StringIndexOutOfBoundsException e) { 
     e.printStackTrace(); 
     Log.e(TAG, Routines.DATABASE_ERROR); 
     showDialogBox(Routines.WARNING, Routines.DATABASE_ERROR); 
    } 
} 

我的问题是如何重复的asynctask,当它完成下载第一个数据,然后重复自我做下一个网址参数。当我检查logcat时,循环总是运行,但任务从未完成。所以,如何让它可以运行重复的asynctask。

+0

如果我正确地读你,就没有必要使用例如10的AsyncTask下载10个网址sequently。就你而言,只需构建并准备10个URL并将URL列表传递到一个AsyncTask中,然后在doInBackground()方法中,正确地实现for循环以按顺序迭代和处理URL。 – yorkw 2012-04-16 10:55:25

回答

0

保持静态计数器和调用的AsyncTask

这样

if(counter<desiredCount){ 


vURL = Routines.URL_CUSTOMER + "?vloop=" + String.ValueOf(vLoop) + "&vsupvid=" + vSpvId; 
     Log.d(TAG, vURL); 
     vCustomerAsyncTask = new CustomerAsyncTask(); 
     vCustomerAsyncTask.execute(vURL); 
} 
中的AsyncTask的 post excecute

..目前什么你做!因为你启动一个的AsyncTask先于其他一个甚至完成可能无法工作..

+0

谢谢你的想法。 – 2012-04-16 09:38:42

+0

@PaijoRX ..做到了.. ..? – ngesh 2012-04-16 09:39:27

+0

但我不知道有多少行,所以我无法初始化静态计数器,我认为静态计数器无法使用。 – 2012-04-16 09:39:47

0

我们无法执行两次或两次以上的AsyncTask。因此,每次我们只在本地创建实例。像new MyTask().execute(url).避免全局声明异步任务实例。另一件事我们可以使用Loader类多次执行异步任务,该类也具有向后兼容性。

+0

我试图读取像这样的数据: – 2012-04-16 09:44:13

0

使用再次调用它像一个倒退的形式`name.excuteTask()”

+0

终于,我找到了解决方案... 只需要在新的参数中再次调用任务,直到停止条件。像递归函数... – 2012-04-17 00:38:57

+0

@PaijoRX:酷!那是什么。你说对了。但要确保你的停车条件不会太长。并检查它是否影响您的应用程序的仪器处理程序。 – 2012-04-17 05:19:47