4

Is there a common way to consume web services from Android applications (aside from the protocol)? What I need is a Service or Intent or Thread (or anything else) that is capable of:

  1. staying alive no matter what Activities are open;
  2. be able to perform multiple requests at the same time;
  3. accept input values and return output values to the right caller;
  4. be able to discard an ongoing operation (while keeping to run all the others) and not to return the output value if it is no longer desired;
  5. (of course) run asynchronously.

Is there a common way to accomplish this? I've had some issues while using Services, primarily because I wasn't able to discard an ongoing operation, so the results were colliding with each other.

frapontillo
  • 10,499
  • 11
  • 43
  • 54

2 Answers2

2

You should use Service for this task. This is the only component that was designed to handle background long-running tasks. Most likely you need to use this service as a "locally bound" to pass complex requests and responses without need for serialization, and to perform other tasks that are relatively hard to implement via Intents.

Read more about locally bound services here: Bound Services.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • I would recommend an AsyncTask over a service for this if the operation is going to be performed in the app. You get the benefit of a clearer structure (onPreExecute, doInBackGround, onPostExecute) and it's a bit simpler to implement. It sounds like this isn't quite what MacGyver wants, but I thought it was useful information. (hence the comment instead of the answer) – Codeman Aug 12 '11 at 15:49
  • The `Binder` in the `Service` is going to have multiple methods, one for each request to the web service. How should I differentiate each callback? Should I pass the `Binder`'s method a Listener? And if I don't need the callback, how do I destroy the listener before the Activity's destroy? – frapontillo Aug 12 '11 at 15:54
  • Why don't you run an AsyncTask in the service? That was you can use a callback from the AsyncTask into your service to call an event handler to do what you want when the service responds. – Codeman Aug 12 '11 at 15:57
  • Personally I do not like to put web-service calls into activity. It complicates it, and now you need to take care about activity lifecycle (like configuration change) in a very careful manner. There are quite some pitfalls there. – inazaruk Aug 12 '11 at 19:41
  • @inazaruk, what are you suggesting to do? – frapontillo Aug 12 '11 at 20:22
  • Stick with locally bound service, its a bit more work at first. But saves a lot of time/headache in long term. – inazaruk Aug 12 '11 at 20:52
1

mmm you need a combination of things here. First of all, you need a service layer implement it with a Bound Service (inherits). Your UI activities must call this service through an Intent. Then, this layer that would be execute it in a different thread than your UI layer, must have a Rest Client object to perform services calls. Here you have lot of approaches, I recommend you Spring Rest implementation as well as Restlet.

As is a pretty big discussion, anything else please tell me.

matiasnj
  • 604
  • 4
  • 13