2

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.

The_Martian
  • 3,684
  • 5
  • 33
  • 61
  • 1
    Shouldn't the type that you declared in your method parameter actually match one of the generic parameters? – Robert Harvey May 27 '15 at 20:49
  • 1
    You are telling `onPostExecute()` to expect a `String` with the third `param` there. Which means, you should be returning a `String` in `doInBackground()` – codeMagic May 27 '15 at 20:57

3 Answers3

3

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);
Teo Inke
  • 5,928
  • 4
  • 38
  • 37
  • You can also just the same thing here with a blank constructor and define a method to populate a class array of strings that is saved in MyAsyncTask and used by doInBackground. – OscillatingMonkey May 27 '15 at 20:56
  • There are many possibilities. I like this one because you create the AsyncTask passing parameters from a Main class with only one line of code – Teo Inke May 27 '15 at 20:58
2

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.

enter image description here

Community
  • 1
  • 1
Mithun
  • 2,075
  • 3
  • 19
  • 26
  • Thanx everyone. I edited my question now so the third parameter matches the return value. For the answer though, I picked @Teo's response for simplicity. I am loving this site by the day :) – The_Martian May 28 '15 at 00:21
1
t.execute(string1, string2, string3, string4);  // as many as you want..

that's the simplest way of passing more than one String arguments to AsyncTask

Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77