Question

I implemented an AsyncTask in my app and encountered exceptions like IllegalArgumentException. I asked question on stack overflow somebody said I shouldn't use AsyncTask in apps for the following reasons.

  • AsyncTasks don't follow Activity instances' life cycle
  • Memory leak issue
  • Progress of your task will be lost

I was recommended Robospice and also told consider this thread AsyncTask is not really the best tool to create network requests on Android. use RoboSpace

Can anybody tell me what is best option to use access network asynchronously? What should I use AsyncTask or RoboSpice?

Était-ce utile?

La solution

According to this guy at Google IO, using AsyncTask for networking is not the way to go. He may give his presentation with an annoying & condescending tone, but nonetheless has some good points.

IMO the leading libraries to use are android-async-http and robospice.

DISCLAIMER: I haven't tried either of these yet. While I like the API of android-async-http better, it seems like Robospice was more recently developed and supports better libraries.

I may end up rolling my own that extends IntentService.

Autres conseils

here is an Android Asynchronous Http Client library file to do these work. Have a try.

http://loopj.com/android-async-http/

I have been using AsyncTasks for a few projects and never had that problems. Nevertheless as Snicolas said in that post, is possible to had problems if you use AsyncTasks for long running operations (note the bold).

So if you're doing short ops, try AsyncTasks, otherwise I would give Robospice a try.

an asyncTask instance just can use one time,if you want to execute the second request must call new () again

I use AsyncTask in all my applications. No problem.

Example in fragment http://pastebin.com/EDZtKy3M

Example base for a custom implementation of CustomTask http://pastebin.com/F18hRXr6

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top