我必须对 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() 函数中计算的值?
你能不这样做吗:
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) {
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)