0

I want to show the progressdialog when the webservice is called and stop the dialog on request is finished. I did the following way but the dialog showing after the web service request is finished.

public class NetWorkRunTask extends AsyncTask<String, Void, String> {
    Context ctx;
    public NetWorkRunTask(Context ctx)
    {
        this.ctx=ctx;
         mProgressDialog = new ProgressDialog(ctx);
    }
    ProgressDialog mProgressDialog;
    @Override
    protected void onPreExecute() {


        mProgressDialog.setMessage("Please wait....");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();

    }

    @Override
    protected String doInBackground(String... params) {
        //contactService.getAssetsAtFirstRun();
//      mProgressDialog.show();
        return ServerConnection.getXmlRespFromUrl(params[0]); //this will include HttpPost 
        //return null;
    }

    @Override
    protected void onPostExecute(String result) {

       if(mProgressDialog != null)
        {
        if(mProgressDialog.isShowing())
          {

               mProgressDialog.dismiss();
               // uti.showToast(getBaseContext(), "Zapisano kontakty.");}

            }
         }
    }

}

and in onClickListener

String xml=null;
                try {                   
                    xml =new NetWorkRunTask(MyActivity.this).execute(finalURL,null,null).get();
                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

what's going wrong here.....

user1557549
  • 51
  • 2
  • 4

4 Answers4

3

just do this in your asynk task

 @Override
    protected void onPostExecute(String result) {

        mProgressDialog.dismiss();
    }
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • I did it.but the dialog is appears only after doInBackground() is finished. – user1557549 Dec 02 '13 at 05:51
  • when u click on your button or what ever it is then asyn task is call if u give dailog.show() in your preexecute then it will show in preexecute itself dude see i give u step that how async class work 1.) onpreexecute() : here we show progress dailog and bulid our credential if it is 2.) doinbackround() : here we do your main task which is time taking task 3.) postexecute() : here we dimiss that progress and after async task what we want to do here we do – Bhanu Sharma Dec 02 '13 at 05:59
1

Remove the get() method, while calling your async task change xml =new NetWorkRunTask(MyActivity.this).execute(finalURL,null,null).get();

to

xml =new NetWorkRunTask().execute(finalURL,null,null);

and

public class NetWorkRunTask extends AsyncTask {

ProgressDialog mProgressDialog;

@Override

protected void onPreExecute() {

mProgressDialog = new ProgressDialog(MainActivity.this);
    mProgressDialog.setMessage("Please wait....");
    mProgressDialog.setIndeterminate(true);
    mProgressDialog.setCancelable(false);
    mProgressDialog.show();

}

@Override
protected String doInBackground(String... params) {
    //contactService.getAssetsAtFirstRun();
    return ServerConnection.getXmlRespFromUrl(params[0]); //this will include HttpPost 

}

@Override
protected void onPostExecute(String result) {

   if(mProgressDialog != null)
    {
    if(mProgressDialog.isShowing())
      {

           mProgressDialog.dismiss();
           // uti.showToast(getBaseContext(), "Zapisano kontakty.");}

        }
     }
}
sijeesh
  • 298
  • 1
  • 15
  • Yes...It works if i remove get(). But i need the response from AsyncTask, So that i can handle the UI according to my requirements.. – user1557549 Dec 02 '13 at 08:50
  • try this : http://stackoverflow.com/questions/9019249/progressdialog-not-shown-when-asynctask-get-called – sijeesh Dec 02 '13 at 09:12
0

In your code you are calling 'get()' method. [get() -waits if necessary for the computation to complete"]. Replace your Asynch task calling line with the below code and try

xml =new NetWorkRunTask(MyActivity.this).execute(finalURL,null,null);

Deniz
  • 12,332
  • 10
  • 44
  • 62
0

You can define an interface in your Async task

    public interface OnProcessCompleteListener{
    public void onSuccess(String result);
    public void onFailure();
}

and in your activity class you can implement the call back methods and can return results to the class.

    OnProcessCompleteListener listener = listener = new OnProcessCompleteListener() {

        @Override
        public void onSuccess(String result) {
        // do what u want   
        }

        @Override
        public void onFailure() {

        }
    };

Pass the 'listener' to the AsyncTask and call the onSuccess(String result), onFailure() methods where u want.

    @Override
    protected void onPostExecute(String result) {

     if(result != null) {
      listner.onSuccess(result);
     }else{
      listner.onFailure();
     }

          if(mProgressDialog != null){
           if(mProgressDialog.isShowing()){

                  mProgressDialog.dismiss();
                 // uti.showToast(getBaseContext(), "Zapisano kontakty.");}

           }
          }
   }
Deniz
  • 12,332
  • 10
  • 44
  • 62