0

我正在实现一个 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 方法(作为参数接收)

4

2 回答 2

1

在实例化 WCHelper 对象期间也发送参数。

在 WCFHelper 类中保留一个变量。

public class WCFHelper extends AsyncTask<Object, Void, String[]>{
    int val;
    public WCHelper(int param)   //constructor
    { 
        super();
        val = param;  //now you can use this val in onPreExecute
    }
...
}

Send the same parameter you wanted to send to onPreExecute(). For example,

WCHelper task = new WCHelper(param);  // this sends value to constructor
task.execute(param);  //this sends value to doInBackground
于 2013-04-18T07:10:49.840 回答
0

您可以创建自己的 AsyncTask 自定义构造函数来传输处理进度对话框所需的参数。

private class MyTask extends AsyncTask<Void, Void, Void> { 

    public MyTask(boolean showProgress){
     ....
    }

    ....
}
于 2013-04-18T06:57:30.800 回答