Using SOAP with Android implementation recommendations
Im trying to connect to开发者_StackOverflow a SOAP service using Android and have read in SO as well as other websites about using ksoap2.
Ive also been reading Android in Action, which indicates that any long running code (> 5 seconds) in a UI thread should be done in either a Handler or as a Service. Are there any recommendations on when to use these as opposed to just using it inside the Activity. Many examples in SO as well as on the internet do SOAP processing right inside the Activity (either in the lifecycle methods or on event handlers) but in my case, I know that there are certain SOAP methods that are going to take more than 5 seconds.
Any pointers or recommendations of when to use Android Handler or Services would be very helpful.
If the UI is depending on the response in that same instance of time, then I recommend you to make use of AsyncTask
class. AsyncTask
are designed and recommended for such long operations. As an assumption that response of the server is strongly related to the UI than you MUST NOT keep the UI frozen while that long operation is in progress.
You can do these long transactions in the doInBackground()
method which will run on background thread, must note that in this method YOU CAN'T UPDATE THE UI, and in onPostExecute()
/onProgressUpdate()
you update the UI with the response from the server.
Read more on AsynTask if you haven't been introduced alread, here:
http://developer.android.com/reference/android/os/AsyncTask.html
Don't forget to mind the Threading Rules
I recommend using AsyncTask instead. Look it up here: http://developer.android.com/reference/android/os/AsyncTask.html
Basically, you can execute your SOAP calls in doInBackground
method, which is executed in a background thread, and you can update your UI in onPostExecute()
method, that way you can avoid the dreaded ANR (Android Not Responding) error.
Have a look at this for an example of AsyncTask.
I suggest you refer the Android Developers page for AsyncTask properly before you begin implementation.
All the best
Cheers
精彩评论