2

I am not able to make a call on AsyncTask and IntentService. I am developing an android app which has user registration activity. When a user enters a username in registration activity, I have to check, whether the username is already taken or not by sending a HTTP request to server. The response from the server is JSON object with usernames.

Please help me in taking call to design background username check AsyncTask/ IntentService with pros and cons.

geekgugi
  • 389
  • 1
  • 6
  • 22

3 Answers3

3

If you're using an AsyncTask then you don't need an IntentService here. If you post your code we can show you what you might be doing wrong, but making an HTTP request on a thread with AsyncTask is pretty straight forward.

Here's a good example of how to use it for this purpose: http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

Basically AsyncTask is just a fancy wrapper around a Thread that allows you to talk back to the display thread. You could achieve the exact same result with a Thread that does your HTTP call and a Runnable that updates your display that your thread can call with a Handler.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
1

from this helpful answer

You should use an AsyncTask for short repetitive tasks that are tightly bound to an activity, like what you're currently trying to do. IntentService are more geared towards scheduled tasks (repetitive or not) that should run on the background, independent of your activity.

so this suggestion also goes with you.

Apart from that I will suggest you to look into the android volley

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
1

IntentService and Asynctask is not comparable anyway.

IntentSerive

  • Android component require registration in AndroidManifest.
  • Usese work queue design pattern. Multiple request put in a queue and processed one by one.
  • Uses its own worker thread to process.
  • Asynchronous: to start, you send and Intent, Android system query its local system to locate appropriate service component. Which means it might take a while for an IntentService to start.
  • It registered in AndroidManifest, means with enabled permissions other apps will be able to access it through Intent.
  • Subclass of Context

AsyncTask

  • Just another fancy java class
  • Must be created and executed from main thread.
  • Uses a background thread for operation.
  • Not accessible outside of its java scope.
  • Execute multiple time require multiple instance
  • Convenient design pattern to do any background task for updating UI
  • You have to pass the context as parameter to use it,
  • Execution start when user call execute(). No Intent resolve require.
minhaz
  • 4,233
  • 3
  • 33
  • 49
  • I assume both run asynchronously, right ? Why not comparable in anyways ? – geekgugi Feb 10 '15 at 07:58
  • I added on line at the end on Asyctask. Comparison and benchmarking works better when, when both designed for same goal. Lets say Service vs IntentService or AsyncTask vs AsyncTaskLoader. – minhaz Feb 10 '15 at 08:59
  • You mean to say goals which can be achieved with AsyncTask/Service can't be achieved with counter part of it ? – geekgugi Feb 10 '15 at 11:43