android - Threaded web service call -


Returns a list of web service items I am requesting information from the web service in my Android app, in each item Base 64 is the image and some information

The first method: Just use the web service and get the results. This means that until the data is downloaded the UI is freeze. Not a good solution

The second method: insert the data in one thread, and display a progress bar with the message

  Private Zero StartGet Data () {viewInfo = New Runnabal () {Public run zero () {getData (); // web service}}; Thread thread = new thread (tap, viewinfo, "MagentoBackground"); Thread.start (); ProgressDialog = ProgressDialog.show (TracksListActivity.This, "Please Wait ...", "Recovering Data From the Web ...");); } Private zero getData () {{Get data in a list from web service and then run ONUTHTHRED (RETURNER); } Private Runnabal Returners = New Runnabal () {Public runs zero () {Pop up list view adapter with information from web service results list and progress. Dieses (); GeneralTrackInfoAdapter.notifyDataSetChanged (); }};  

This shows a good loading image and message that the user will have to wait until the full download. I do not know how to cancel the getdata () thread.

The third way: I have to like something, the user will press a button to get the data, Thread download items serve the item from the web and show it in the immediate list. Usage can always cancel request with button press.

But, how to do it? Or is there another way?

You can use asyncTask for threading, the basics: , Examples:.

Because using a class thread or interface makes your code more complex and more difficult to read.

The aim of Asinka Task is to take care of thread management.

The basic structure is:

  The public class downloader task extends asyncTask & lt; String, string, string & gt; {Protected void onPreExecute () {} protected string doInBackground (string .... args) {// do something in the background} Secure void onProgressUpdate {string ARR} {} protected void onPostExecute (string result) {//..update ui }}  

... to start your work such as:

  click on Public Zero (see V) {New Downloader Task () . Execute ("param");  
  • The method doInBackground () is automatically executed on a worker thread

  • onPreExecute (), onPostExecute ()

  • The value given by doInBackground () is sent to onPostExecute ()

  • Time can cancel the job from any thread

  • Thread

  • / P>


Comments