如何避免链接几个AsyncTask调用?

时间:2014-03-10 14:39:10

标签: java android

我必须多次调用Web服务,但每一步都使用上一步中的值,所以现在我有一个庞大的AsyncTasks链:每个AsyncTask都在AsyncTask的onPostExecute()中执行。前一步。这非常非常难看,很难修改。我怎么能避免这个?我想将每一步都放在一个单独的函数中:

int step5() //this function is on the main UI thread
{
   return getStep5ValuesFromWebService() + valuesFromPreviousSteps;
}

int getStep5ValuesFromWebService()
{
   //do work on new thread
   //GetValueFromService(); <-- this is the function that returns an int from the web service, but it has to be called from another thread
}

如何调用GetValueFromService()以便step5()函数返回GetValueFromService()函数中计算的值?

1 个答案:

答案 0 :(得分:0)

你不能这样做:

private class MyAsync extends AsyncTask<String, Integer, Long> {

    protected Long doInBackground(String... symbols) {
        step1();
        step2();
        step3();
        step4();
        step5();
    }

    private void step1(){}
    private void step2(){}
    private void step3(){}
    private void step4(){}
    private void step5(){}

    protected void onProgressUpdate(Integer... progress) {
    }

    protected void onPostExecute(Long result) {
    }
}