This must be really easy but I am stuck now for an hour or so. I am passing String[]
to an AsyncTask
class as such
class test extends AsyncTask<String, Void, Void>{
@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
}
}
I am sending my arguments as such
test t = new test();
String[] s = {"a", "b", "c", "d", "e"};
t.execute(s);
This is not working. How would I pass multiple String
objects is my question. If I pass one string it works but if I try to pass them in an array it fails. btw I don't want to change the string parameter of the AsyncTask
class to String[]
because it would break my other code. Any help would be appreciated.
If you want to pass multiple Objects to this AsyncTask you can create a constructor to match them.
private class MyAsyncTask extends AsyncTask<Void, Void, Integer>
{
public AsyncFileExists(Integer num1, Integer num2, String s, Boolean b) {
super();
// Do something with these parameters
}
@Override
protected void onPreExecute() { }
@Override
protected Integer doInBackground(Void... params) {
...
And then just do
MyAsyncTask myTask = new MyAsyncTask(5, 10, "a string", false);
Are you sure it's not working? Pardon, if I am missing something!
String[] s = { "a", "b", "c", "d", "e" };
String[] s1 = new String[]{ "a", "b", "c", "d", "e" };
both result in [a, b, c, d, e]
In doInBackground(String... params)
you are expecting String varargs
. So you can basically pass zero or more
String
objects (or an array
of them) as the parameter(s) for your function doInBackground
. See here .
t.execute(string1, string2, string3, string4); // as many as you want..
这是将多个String
参数传递给AsyncTask
的最简单方法
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.