Search code examples
androidandroid-asynctaskparametersparams-keyword

How do I change (String ...params) argument list in doInBackground asyncTask in android?


by default from api for asyncTask the signature for it is doInBackground(Param ...params)

in my app I have this signature:

public String doInBackground(String ...params) 

I tried and changed to this:

public String doInBackground(String param, int x) 

but it gives me error:

class DataTasker must be declared abstract or implement abstract method 'doInBackground(Params...params)

I know this 3 dots is an array and i can access it like

params[0] , params[1].

But still confused, in my main activity class I want to pass this data for background task: a string , an integer

DataTasker data = new DataTasker() ;
data.execute("mister x " , 56) ;

But apparently, i must pass only one argument


Solution

  • You can try ,

    Convert the integer into String and pass it wih the "mister x"

    DataTasker data = new DataTasker() ;
    data.execute("mister x " , String.valueOf(56)) ;
    

    You can access both values as params and convert the second parameters into integer again

    @Override
    protected JSONObject doInBackground(String... params) {
    
        String user=params[0];
        int value = Integer.parseInt(params[1]);
    }