里面的AsyncTask的Android的AsyncTask [英] Android AsyncTask inside AsyncTask
问题描述
所以,我正在一家酒吧code德codeR,这一次,我们有条code去倍数API在互联网上脱code什么只是扫描。 事情是,我必须链接一些XML解析在一起,我不知道如果我这样做是正确的。
So, I'm working on a barcode decoder, which once we have the barcode goes to multiples API over the internet to decode what was just scanned. The thing is that I have to link some XML parsing together, and I don't know if I'm doing it right.
所以,一旦吧code扫描,我的程序调用AsyncTask的肚里了一个API来检索产品名称。一旦它的名字,我想它调用另一个AsyncTask的。我知道这是可能通过实例的 onPostExecute()
其他的ASyncTaks但是,我认为这是错误的,因为它像框内框。
所以,是不是有可能/最好实例我第二次的AsyncTask里面我的主要活动,并使其等到我的第一个AsyncTask的完成?
So, once the barcode is scanned, my program calls an ASyncTask which goes over an API to retrieve the product name. Once it has the name, I want it to call another ASyncTask. I know this is possible by instantiating an ASyncTaks in the onPostExecute()
of the other but, I think this is wrong, because it's like boxes within boxes.
So isn't it possible/better to instantiate my second ASyncTask inside my main Activity, and make it wait until my first ASyncTask is finished ?
(英语不是我的母语,我希望我自己清楚)。
(english isn't my primary language, I hope I made myself clear).
推荐答案
我认为这是绝对合法的,开始第二个的AsyncTask
在 onPostExecute
,混合这两种操作是一个糟糕的逻辑思路,为子孙之称的 - 你得陪他们分离
I think it's absolutely legitimate to start the second AsyncTask
in the onPostExecute
of the first AsyncTask, Mixing both operations is a bad logical idea, As "The Offspring" said - "You've gotta keep 'em separated"
如果你不希望它是直接在 onPostExecute
本身,设置一个处理程序中的活动执行,并从 onPostExecute
。
If you don't want it to be directly inside the onPostExecute
itself, set a handler to execute it in the activity and call this handler from onPostExecute
.
和最后一件事 - 如果你有大量的逻辑 - 将其移动到一个单独的文件,不要让这一切在同一文件
And last thing - If you have a lot of logic - move it to a separate file, don't keep it all at the same file.
这篇关于里面的AsyncTask的Android的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!