Android的:替代的AsyncTask? [英] Android: Alternatives to AsyncTask?
问题描述
对于我的应用我做了一个框架,为所有网络调用外部API。我把一切都放在服务,如: UserService
, MessageService
等,所以现在当我想调用一个网络法我不能在我的活动的UI线程做
所以我做了一个的AsyncTask
,一切都只是罚款=)
for my app I made a framework for all network calls to an external API. I put everything in "Services" like: UserService
, MessageService
etc. So now when I want to call a network method I can't do this in the UI-Thread of my Activity
so I made an AsyncTask
and everything was just fine =)
但现在我要像给方法执行到的AsyncTask
所以我并不需要编写一个的AsyncTask
我的网络架构的每一个方法。因此,而不是有这样的事情:
But now I want something like giving the method to execute to the AsyncTask
so I don't need to write an AsyncTask
for every method of my network-framework. So instead of having something like this:
public class NetworkTask extends AsyncTask<UserService, Void, Void> {
@Override
protected Void doInBackground(UserService... params) {
UserService userService = params[0];
userService.getUser();
return null;
}
}
我想要更多的东西抽象,让的AsyncTask
不需要知道的getUser的方法?但我无法找到如何做到这一点任何想法......也许AsyncTask的并不好这个呢?
I want something more "abstract" so the AsyncTask
doesn't need to know the "getUser" method? But I can't find any idea of how to do this... Maybe the AsyncTask is not good for this anyway?
推荐答案
记住,Android有线程和AsyncTasks的限制。
Remember that Android has a limit of threads and AsyncTasks.
您可以使用2库做AsyncTasks的同样的工作:
You can use 2 libraries to do the same work of AsyncTasks:
1)排球 - > 的http:// www.androidhive.info/2014/05/android-working-with-volley-library-1/
1)Volley -> http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
2)循环J - > http://loopj.com/android-async-http/一>
2)LoopJ -> http://loopj.com/android-async-http/
好运气在你的选择。
问候,
Uilton。
这篇关于Android的:替代的AsyncTask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!