How to pass more values in the doInBackground
My AsyncTask
looks like this.
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
{
}
}
Is it possible somehow to pass more values on my protected String DoInBackground
for example: protected String doInBackground(String... sUrl, String... otherUrl, Context context)
And how to execute
the AsyncTask
after? new DownloadFile.execute("","",this)
or something?
you can send multiple parameters as you can send them as varargs. But you have to use same Type of parameter. So to do what you are trying you can follow any of the followings
Option 1
you can use a setter method to set some value of the class member then use those in doInBackGround. For example
Option 2
Or you can use constructor to pass the values like
Ypu can create constructor to pass different type parameters and also strings data using String......str
You cannot, for the following reasons :
is not a valid method signature. The dots notations (Varargs) can only be used as the last parameter of the method. This restriction is because otherwise it would make polymorphism much more complex. In fact, how would java know which of your Strings go to
sUrl
, and which goes tootherUrl
?Moreover,
doInBackground
overrides a method fromAsyncTask
. As such, you cannot change the method signature.What you can do, however, is make those values members of your class and pass in the constructor of your
DownloadFile
class or add setters to set them before callingexecute
.you can do something like this inside your doInBackground method:
execute AsyncTask in this way:
the first value : sUrl[0] will be the one passed from string1 and
surl[1] will be the second value passed i.e string2 !
the three consecutive dots meaning more then one String. The dots are varargs
you can force it adding a constructor that takes the Context as parameter: