jsp
jsp

Reputation: 2636

AsyncTask: Using web services & threads

I am using AsyncTask on button click to refresh the screen. Following is the sequence of events that happen on btn click

  1. progress dialog shows up
  2. The doInBackground is called and thread is initialized which calls a web service. The web service fetches/uploads data. A pass/fail flag is set once the web service is called.

My problem is the onPostExecute is never called and therefore the screen is never refreshed. And secondly by the time the data is downloaded and the web service sets the flag my code has already hit return stmt in doInBackground.

Question is how do i stop execution in my asynctask so that the web service is done downloading/uploading the data and finally execute onPostexecute.

FYI I also get the following warning in eclipse

The method onPostExecute(boolean) from the type Screen.ConnectWebService is never used locally

private class ConnectWebService extends AsyncTask <Void, Void, Boolean>
        {
            private final ProgressDialog pd = new ProgressDialog(screen.this);

            protected void onPreExecute() {
                pd.show(Screen.this, "Sync", "Sync in progress",true,false);
            }


            protected Boolean doInBackground(Void... unused) {

                if (SyncInProgress == false)
                {

                    CallWSThread();//creates thread which  calls web service
                }
                        Log.d("doInBackground","doInBackground");
                return SyncStatus;
            }

            protected Void onPostExecute(boolean result)
            {   

                pd.dismiss();
                if (result==true) drawRadioButtons();

                 return null;
            }

        }

Upvotes: 0

Views: 3020

Answers (2)

mnemy
mnemy

Reputation: 679

As djg noted, you have a typo in your method declaration. You can avoid these kinds of mistakes by using the annotation @Override when you're implementing methods from a super class.

Upvotes: 1

djg
djg

Reputation: 1283

It should be:

protected Void onPostExecute(Boolean result)

Upvotes: 4

Related Questions