我正在实现一个 AsyncTask,它在 doInBackground 方法中调用 WCF 服务方法。
WCF 方法名称是 doInBackground 方法的参数。
我希望进度对话框仅显示发送到 doInBackground 的特定方法名称。
我的 porgressdialog 设置是在 onPreExecute 方法中设置的。
为特定 doInBackground 参数(wcf 方法名称)显示progressdialog 的任何方式
public class WCFHelper extends AsyncTask<Object, Void, String[]>{
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://url";
final ProgressDialog pd;
public Context ctx;
public WCFHelper(Context _ctx)
{ this.ctx = _ctx;
pd = new ProgressDialog(_ctx);
}
@Override
protected void onPreExecute() {
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("login...");
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
}
@Override
protected String[] doInBackground(Object... params) {
String WCFmethod = (String)params[0];
Map<String,Object> parameterArgs = (Map<String,Object>)params[1];
Boolean isArr = (Boolean)params[2];
String [] Fail = {"Fail"};
String SOAP_ACTION = "http://tempuri.org/IService1/"+WCFmethod;
String METHOD_NAME = WCFmethod;
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
for(Map.Entry<String,Object> entry: parameterArgs.entrySet())
request.addProperty(entry.getKey().toString(), entry.getValue().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
if(!isArr)
{
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
String[] toReturn = new String[1];
toReturn[0] = result.toString();
return toReturn;
}
else{
envelope.implicitTypes = true ;
SoapObject listDataSet = (SoapObject) envelope.bodyIn;
int numOfStrings = ((SoapObject)listDataSet.getProperty(0)).getPropertyCount();
String[] toReturn = new String[numOfStrings];
for ( int i = 0 ; i <numOfStrings; i++) {
toReturn[i] = ((SoapObject)listDataSet.getProperty(0)).getProperty(i).toString();
}
return toReturn;
}
}
catch (Exception e) {
return Fail;
}
}
protected void onPostExecute(String res) {
// TODO: check this.exception
// TODO: do something with the feed
if(this.pd.isShowing())
pd.dismiss();
}
如您所见,doInBackground 的参数之一是 WCF 方法名称。我希望进度对话框仅显示给特定的 WCF 方法(作为参数接收)