我想在调用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
上onPostExecute:
@Override
protected void onPostExecute(Object obj)
{
//dialog.dismiss();
myMethod(obj);
}
private Object myMethod(Object myValue)
{
//handle value
return myValue;
}
doInBackground:
@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;
}
答案 0 :(得分: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();
}
}
答案 1 :(得分:1)
根据评论,您在get()
上致电AsyncTask
。这将阻止提交线程(在您的情况下为主UI线程),直到异步任务结果可用,即doInBackground()
返回。
移除对get()
的来电并处理完成情况,例如在onPostExecute()
或使用回调函数。