我必须多次调用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
}
Run Code Online (Sandbox Code Playgroud)
如何调用GetValueFromService()以便step5()函数返回GetValueFromService()函数中计算的值?
你能不做吗:
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
106 次 |
| 最近记录: |