How To Pass A Parameters To AsyncTask (Ksoap2)

半城伤御伤魂 提交于 2019-12-04 21:50:41

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.

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

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;
}
Sabian

Let the class Login extend AsyncTask<String, Void,String> and change doInBackground(Void... params) to doInBackground(String... params). Now you can execute the task in the wanted way, new Login().execute(URL,NAMESPACE,METHOD,USERNAME,USERPASS);, and access the given parameters via the params[] array. This means for your example: params[0] == URL, params[1] == NAMESPACE and so on.

codeMagic

The first thing is you need to change

public class Login extends AsyncTask<Void, Void, String>

to

public class Login extends AsyncTask<String, Void, String>

and change

doInBackground(Void...

to

doInBackground(String...

Here is some very helpful documentation on it. If you are still having problems, be a little more specific as to what is or isn't working about it.

The three types used by an asynchronous task are the following:

Params, the type of the parameters sent to the task upon execution.

Progress, the type of the progress units published during the background computation.

Result, the type of the result of the background computation.

These are the paramaters in <String, Void, String>. The first String is what is being passed which is why in doInBackground() you have String... that indicates and array of strings being passed

you can pass as many params as you want in execute because doInbackground(Params... params) (think of it as Params[] params) accepts as many parameters as you want as long as they are of the same type.

but if your parameters are of different types (which isn't your case) you need to have them as attributes for asynctask class and pass their values through your asynctask constructor as new login(type1 attr1, type2 attr2).execute(params) :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!