0

我想在调用 Web 服务调用时显示 ProgressDialog,这是我的代码:

public class penAPIController extends AsyncTask<Object, Void, Object>{

private View view;
private ProgressDialog dialog;

public penAPIController(View v)
{
    view = v;
}
    protected void onPreExecute() 
    {
        this.dialog = new ProgressDialog(view.getContext());
        this.dialog.setMessage("Loading, Please Wait..");
        this.dialog.setCancelable(false);
        this.dialog.show();
    }

该对话框确实显示,但只有在 doInBackground 完成后,我希望能够在 doInBackground 完成其工作时显示它。然后将其隐藏在 PostExecute

onPost执行:

@Override
        protected void onPostExecute(Object obj)
        {
            //dialog.dismiss();
            myMethod(obj);
        }

    private Object myMethod(Object myValue)
    {
         //handle value 
         return myValue; 
    }

做背景:

@Override
        protected Object doInBackground(Object... objects)
        {
            if(objects.length < minNumberOfParams)
            {
                return null;
            }
            Object finalObject = null;
            // TODO Auto-generated method stub
            String NAMESPACE = "http://...";
            String METHOD_LOGIN_NAME = "Login";
            String SOAP_LOGIN_ACTION = "http://...";
            String METHOD_RUNACTION_NAME = "RunAction";
            String SOAP_RUNACTION_ACTION = "http://...";
            String CLIENT = (String)objects[0];
            String APPLICATION = (String)objects[1];
            String USERNAME = (String)objects[2];
            String PASSWORD = (String)objects[3];
            String URL = (String)objects[4];
            String ACTION_NAME = (String)objects[5];
            ArrayList arrayParams = null;
            if(objects.length == (minNumberOfParams + 1))
            {
                arrayParams = (ArrayList)objects[6];//Build parameters xml from ActionParam array
            }
            String PARAMETERS = buildParametersXML(arrayParams);

            SoapObject Request = new SoapObject(NAMESPACE, METHOD_LOGIN_NAME);

            //Client
            PropertyInfo propertyClient = new PropertyInfo();
            propertyClient.setName("client");
            propertyClient.setValue(CLIENT);
            propertyClient.setType(CLIENT.getClass());
            Request.addProperty(propertyClient);
            //Application
            PropertyInfo propertyApplication = new PropertyInfo();
            propertyApplication.setName("application");
            propertyApplication.setValue(APPLICATION);
            propertyApplication.setType(APPLICATION.getClass());
            Request.addProperty(propertyApplication);
            //Username
            PropertyInfo propertyUsername = new PropertyInfo();
            propertyUsername.setName("username");
            propertyUsername.setValue(USERNAME);
            propertyUsername.setType(USERNAME.getClass());
            Request.addProperty(propertyUsername);
            //Password
            PropertyInfo propertyPassword = new PropertyInfo();
            propertyPassword.setName("password");
            propertyPassword.setValue(PASSWORD);
            propertyPassword.setType(PASSWORD.getClass());
            Request.addProperty(propertyPassword);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(Request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try
            {
                androidHttpTransport.call(SOAP_LOGIN_ACTION, envelope);
                SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                String token = response.toString();

                SoapObject RequestRun = new SoapObject(NAMESPACE, METHOD_RUNACTION_NAME);

                //Token
                PropertyInfo propertyToken = new PropertyInfo();
                propertyToken.setName("token");
                propertyToken.setValue(token);
                propertyToken.setType(token.getClass());
                RequestRun.addProperty(propertyToken);
                //Action Name
                PropertyInfo propertyAction = new PropertyInfo();
                propertyAction.setName("actionName");
                propertyAction.setValue(ACTION_NAME);
                propertyAction.setType(ACTION_NAME.getClass());
                RequestRun.addProperty(propertyAction);
                //Parameters
                PropertyInfo propertyParams = new PropertyInfo();
                propertyParams.setName("parameters");
                propertyParams.setValue(PARAMETERS);
                propertyParams.setType(PARAMETERS.getClass());
                RequestRun.addProperty(propertyParams);

                SoapSerializationEnvelope envelopeRun = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelopeRun.dotNet = true;
                envelopeRun.setOutputSoapObject(RequestRun);
                HttpTransportSE androidHttpTransportRun = new HttpTransportSE(URL);
                androidHttpTransportRun.call(SOAP_RUNACTION_ACTION, envelopeRun);
                SoapPrimitive responseRun = (SoapPrimitive)envelopeRun.getResponse();
                String result = responseRun.toString();
                finalObject = parseOutputXML(result);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            return finalObject;
          }
4

2 回答 2

1
   private ProgressDialog progressDialog;   // class variable       

   private void showProgressDialog(String title, String message)
   {
        progressDialog = new ProgressDialog(this);

        progressDialog.setTitle(""); //title

        progressDialog.setMessage(""); // message

        progressDialog.setCancelable(false);

        progressDialog.show();
   }         

onPreExecute()

    protected void onPreExecute()
    {
        showProgressDialog("Please wait...", "Your message");
    }

检查并关闭 onPostExecute() -

    protected void onPostExecute() 
    {
        if(progressDialog != null && progressDialog.isShowing())
        {
            progressDialog.dismiss();
        }
     }
于 2013-05-24T12:15:36.683 回答
1

根据评论,您正在调用get(). AsyncTask这将阻塞提交线程(在您的情况下为主 UI 线程),直到异步任务结果可用,即doInBackground()返回。

删除对完成的调用get()并处理完成,例如在回调函数中onPostExecute()或使用回调函数。

于 2013-05-27T11:55:44.690 回答