如何通过一个参数要AsyncTask的(Ksoap2) [英] How To Pass A Parameters To AsyncTask (Ksoap2)

查看:132
本文介绍了如何通过一个参数要AsyncTask的(Ksoap2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我想传递一个变量的AsyncTask

Hi All I want to pass a Variables to AsyncTask

我有这个变量

private static String NAMESPACE = "aaa";
private static String METHOD_NAME = "bbb";
private static String SOAP_ACTION =  NAMESPACE + METHOD_NAME ;
private static String URL = "ccc";

和我有这样的任务

    public class Login extends AsyncTask<Void, Void, String>
    {
    ProgressDialog progress;
String response = "";
    private ProgressDialog pDialog;
public void onPreExecute() 
  {
    super.onPreExecute();
    pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Please Wait");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();
  }
    @Override
protected String doInBackground(Void... arg0)       {
         final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);             
         request.addProperty("username", user_name);
         request.addProperty("userpass", user_pass);
         final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.setOutputSoapObject(request);
         envelope.dotNet = true;
         try 
            {
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    androidHttpTransport.call(SOAP_ACTION, envelope);                    
                    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();          
                    response = result.toString();
            }
         catch (IOException e) 
            {
             response = "Error In The Operation(1) !!\n Check Internet Connection And TRY AGAIN.";
            }
         catch (Exception e) 
            {
             response = "Error In The Operation(2) !!\n Check Internet Connection And TRY AGAIN.";
            } 
    return response;
    }
@Override
public void onPostExecute(String res)
{
            if(!(res.equalsIgnoreCase("")))
            {
                     if (res.toString().contains(",") == true)
                     {
                   String[] separated = res.split(",");
                   tv.setText(separated[1]);
                   return;
                     }

                 if(res.toString().equals("1"))
                 {
                     res = "Wrong User name OR password ,, TRY AGAIN ..";
                     tv.setText(res);
                     pDialog.dismiss();
                     return;
                 }
                 if(res.toString().equals("2"))
                 {
                     res = "Your Account Is temporarily Blocked ,, Please Call The Admin";
                     tv.setText(res);
                     pDialog.dismiss();
                     return;
                 }
                 if(res.toString().equals("3"))
                 {
                     res = "Error While Retrieve S Information ,, Try Again Later .";
                     tv.setText(res);
                     pDialog.dismiss();
                     return;
                 } 
                tv.setText(res);
                pDialog.dismiss();
            }
}
    }

我需要时,我想执行此TAKS

I Need When I Want To Execute this Taks

要调用它,并且通过上述变量

To Call It And Pass The Above Variables

如同

new Login().execute();

请它

new Login().execute(URL,NAMESPACE,METHOD,USERNAME,USERPASS);

通过Knolledge这个任务返回一个字符串:)

With Knolledge That this task return a String :)

以及 doInBackground 必须有一个用户名和安培值; user_pass需要通过它执行呼叫。

AND THE doInBackground MUST HAVE a value for user_name & user_pass Need To Pass It With Execution Call ..

问候...

推荐答案

为什么不创建一个构造函数登录类,如下面?就我而言,我传递的活动,以我的AsyncTask,这样我可以调用回调函数完成后,但在你的情况,你也可以通过你的字符串数组。

Why not create a Constructor for the Login class, like below? In my case, I'm passing an Activity to my AsyncTask so that I can call a callback function when done, but in your case, you could also pass an Array of your Strings.

在下面这种情况下,args数组传递到类构造而参数数组传递到doInBackground功能。所述MainActivity被传递到的AsyncTask使得taskDone回调可以被称为在MainActivity一旦任务完成

In this case below, the args array is passed to the class constructor while the params array is passed to the doInBackground function. The MainActivity is passed to the AsyncTask so that the taskDone callback can be called in MainActivity once the task completes.

public class Login extends AsyncTask<String, Void, String>
{
    private MainActivity activity;

    //These private strings are only needed if you require them
    //outside of the doInBackground function.... 
    //If not, just use the params argument of doInBackground by itself
    private String METHODNAME,
    private String NAMESPACE;
    private String SOAPACTION;
    private String USER_NAME;
    private String USER_PASS;

    public Login(String[] args, MainActivity activity) {
        this.NAMESPACE= args[0];
        this.METHODNAME = args[1];
        this.SOAPACTION = args[2];
        this.USER_NAME = args[3];
        this.USER_PASS= args[4];

        this.activity = activity;
    }
    @Override
    protected Void doInBackground(String... params) {
        //Again, use either params local to this function
        //or args local to the entire function... 
        //both would be redundant
        String _NAMESPACE = params[0];
        String _METHODNAME = params[1];
        String _SOAPACTION = params[2];
        String _USER_NAME = params[3];
        String _USER_PASS= params[4];

        //Do background stuff
    }

    protected void onPostExecute() {
        //dismiss progress dialog if needed
        //Callback function in MainActivity to indicate task is done
        activity.taskDone("some string");
    }
}

MainActivity.java

MainActivity.java

private String[] args= {"mynamespace", "mymethods", "mysoap", "myuser", "mypass"}; //to pass to constructor
private String[] params= {"mynamespace", "mymethods", "mysoap", "myuser", "mypass"}; //to pass to doInBackground

//Pass your args array and the current activity to the AsyncTask
new Login(args, MainActivity.this).execute(params);

//Callback for AsyncTask to call when its completed
public void taskDone(String returnVal) {
    //Do stuff once data has been loaded
    returnText = returnVal;
}

这篇关于如何通过一个参数要AsyncTask的(Ksoap2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆