Synchronous service calls in Android

Olek picture Olek · Feb 9, 2012 · Viewed 8.6k times · Source

I'm working on application whose main responsibility is to contact remote server and display the data provided.

Service is Soap based. For that I use ksoap library, but let's cut to the case.

I've been "calling service" with the use of asynchronous tasks. Everything seemed to go well, but...

Service is sequential, and tends to 'lose' my requests, so I don't get proper results.

So I decided to take a synchronous approach to resolve the issue, but this way I have to provide additional loading buttons/bars etc.

The performance is terrible in this way. What is the best way to handle such case ? What kind of synchronisation can I use so there won't be any race between the requests ?

How can I make use of Android Services ? How are those better?

Thank you in advance for answers.

Answer

Heiko Rupp picture Heiko Rupp · Feb 9, 2012

You can actually call the AsyncTask in a sync way:

class MyTask extends AsyncTask<Void,Void,String>
{...}

MyTask x = new MyTask();
String result = x.execute().get();

See the docs page for AsyncTask