[英]Showing ProgressDialog on AsyncThread Android
I want to show a ProgressDialog when a call to a Web Service call is made, this is my code: 我想在调用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();
}
The dialog shows indeed but only after doInBackground is finished, I want to be able to show it while doInBackground is doing its job. 该对话框确实显示,但只有在doInBackground完成后,我希望能够在doInBackground正在完成其工作时显示它。 And then hide it on PostExecute
然后将其隐藏在PostExecute上
onPostExecute: onPostExecute:
@Override
protected void onPostExecute(Object obj)
{
//dialog.dismiss();
myMethod(obj);
}
private Object myMethod(Object myValue)
{
//handle value
return myValue;
}
doInBackground: 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;
}
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() onPreExecute()
protected void onPreExecute()
{
showProgressDialog("Please wait...", "Your message");
}
Check and dismiss onPostExecute() - 检查并解除onPostExecute() -
protected void onPostExecute()
{
if(progressDialog != null && progressDialog.isShowing())
{
progressDialog.dismiss();
}
}
Based on the comments, you are calling get()
on the AsyncTask
. 根据注释,您在
AsyncTask
上调用get()
。 This will block the submitting thread (main UI thread in your case) until the async task result is available, that is, doInBackground()
returns. 这将阻止提交线程(在您的情况下为主UI线程),直到异步任务结果可用,即
doInBackground()
返回。
Remove the call to get()
and handle the completion eg in onPostExecute()
or using a callback function. 删除对
get()
的调用并处理完成,例如在onPostExecute()
或使用回调函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.