0

I'm writing an application for Android that retrieve data from the web and reformat it for the user. The user specify some input data, HttpClient connect to the site, and return objects representing the data asked by the user.

The network part is obviously too slow to be runt in the main thread, so I thought about splitting it in a different thread, maybe as a Service. So I have a design question:

1) Should I start a new HttpClient for each request in a new thread, or should I build a Service which deals with Http and return the data, to be runt in a separate thread?

2) Which is the easiest way to communicate between thread or activities for an application like mine?

Cheers

Mascarpone
  • 2,516
  • 4
  • 25
  • 46
  • If you've stumbled across this question because of the title, but the answer isn't appropriate to your situation, see this duplicate question: http://stackoverflow.com/q/5422703/50151 – Tom Wright Jul 12 '12 at 18:40

1 Answers1

2

An AsyncTask would be a better choice. That way you could update the UI with a progress bar from within your call.

Details at http://developer.android.com/reference/android/os/AsyncTask.html

Al Sutton
  • 3,904
  • 1
  • 22
  • 17
  • yes I thought that, but it would be a waste of memory... A lot of objects are instantiated for HttpClient and parser... +1 for simplicity -1 for performance.... – Mascarpone Jan 27 '11 at 17:02
  • In all fairness, your question was and I quote "Which is the easiest way to communicate between thread or activities for an application like mine?" -- He probably gave you the easiest way. – user432209 Jan 27 '11 at 17:41
  • 1
    There's nothing to stop you having a singleton (which you should probably wrap with a factory class) which handles the parsing and is used by the AsyncTask. An AsyncTask will be a lot more lightweight than having a Service. – Al Sutton Jan 28 '11 at 07:05