I want to show a ProgressDialog when a call to a Web Service call is made, this is my code:
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. And then hide it on 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;
}
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");
}
Check and dismiss onPostExecute() -
protected void onPostExecute()
{
if(progressDialog != null && progressDialog.isShowing())
{
progressDialog.dismiss();
}
}
Based on the comments, you are calling get()
on the AsyncTask
. This will block the submitting thread (main UI thread in your case) until the async task result is available, that is, doInBackground()
returns.
Remove the call to get()
and handle the completion eg in onPostExecute()
or using a callback function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.