0

I have this asynctask working fine for what I want with a single argument (remove the param2 and it runs) but as soon as I attempt to add the 2nd argument I receive:

Syntax error on token "param2", VariableDeclaratorId expected after this token

which to be honest, I've never come across.

The function is below (havent included the parameters as I know they've worked in other functions and do work when used individually, but as a pair....) I believe I may be trying to add them incorrectly?

Do I need to make them into an array and use the array as a parameter? If so, how would I go about it? (Still getting the hang of android!)

My function

private class LoadList extends AsyncTask<String, Void, ArrayList<List>> {
    private Exception exception = null;
    /**
     * Main worker method
     */
    protected ArrayList<List> doInBackground(String... param1, param2) { 
        try {
            //Call web service
            return Utils.getWebService(getApplicationContext()).getListInfo(param1[0], param2[1]);
        } catch (Exception e) {
            exception = e;
            return null;
        }
    }
}

If any more is needed let me know, please, and thank you!

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • This is not an android-specific problem: 1) you need to understand *java varargs* and if they are the right thing to use here... 2) do you know what *AsyncTask>* means? – DaniEll Oct 01 '14 at 08:25
  • it looks like you come from another world : PHP !! `param2` needs to be typed. [Read this link] (http://stackoverflow.com/questions/766559/when-do-you-use-varargs-in-java) – S.Thiongane Oct 01 '14 at 08:31
  • I understand the basic theory behind AsyncTask and its construction, just passing multiple String parameters confused me @DaniEll ... – Jay Paleschi Oct 01 '14 at 08:35
  • Also, on a sidenote.... being voted down would be helpful if a comment was left explaining why, so I don't make whatever mistake I made again.... – Jay Paleschi Oct 01 '14 at 12:50

4 Answers4

1

There is two ways to pass a variable to the AsyncTask.doInBackground method:

  • using it's varargs parameter : String... param. You can add many value to param when calling the execute method:

    LoadList loadistTask = new LoadList(); loadistTask.execute(new String[]{"my value","another value"}); After you will access them like this: param[0] , param[1], etc.

  • The other method is to create a custom constructor and pass your variable to it:

    LoadList loadistTask = new LoadList("my var here"); loadistTask.execute();

    private class LoadList extends AsyncTask<String, Void, ArrayList<List>> {
    private Exception exception = null;
    private String myVar;
    /**
    * constructor
    */
    public LoadList(String myVar) {
        this.myVar = myVar;
    }
    
    /**
     * Main worker method
     */
    protected ArrayList<List> doInBackground(String... param) {
        // this.myVar to access your var in the doInBackground method.
        try {
            //Call web service
            return Utils.getWebService(getApplicationContext()).getListInfo(param[0], param[1]);
        } catch (Exception e) {
            exception = e;
            return null;
        }
    }
    
S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
0
private class LoadList extends AsyncTask<String, Void, ArrayList<List>> {
private Exception exception = null;
/**
 * Main worker method
 */
protected ArrayList<List> doInBackground(String... param) { 
    try {
        //Call web service
        return Utils.getWebService(getApplicationContext()).getListInfo(param[0], param[1]);
    } catch (Exception e) {
        exception = e;
        return null;
    }
}

Try this

Mahesh
  • 974
  • 6
  • 12
0

You will just need to pass an array of String.

Snippet:

AsyncTask:

private class LoadList extends AsyncTask<String, Void, ArrayList<List>> {
    private Exception exception = null;
    /**
     * Main worker method
     */
    protected ArrayList<List> doInBackground(String... params) { 
        try {
            //Call web service
            return Utils.getWebService(getApplicationContext()).getListInfo(param1[0], param2[1]);
        } catch (Exception e) {
            exception = e;
            return null;
        }
    }
}

Call it as:

LoadList loadistTask = new LoadList();
loadistTask.execute(new String[]{"value 1","value 2"});

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Thats how I thought it would need to be done! A previous answer worked for me also, but thank you for giving me that! – Jay Paleschi Oct 01 '14 at 08:36
0

Just call your AsyncTask with several arguments, like this:

new LoadList().execute("value 1", "value 2")

The thing is that the argument String... param can handle any number of arguments and bundles them in an array. See Mahesh's answer on how to use them. If you add another argument after having String... param the compiler won't know when to assign a variable of a call to that argument.

So what you can do technically in Java is foo(String a, String b, String... c) but you can't do foo(String a, String... b, String c).

However, in your case, since doInBackground(String... param) is a predefined method signature you won't be able to add more arguments, because the framework wouldn't be able to call it.

Chnoch
  • 772
  • 9
  • 20