Reputation: 4179
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?
Upvotes: 9
Views: 25687
Reputation: 11
new DownloadFile().execute("my url","other parameter or url");
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
{
try {
return downloadContent(sUrl[0], sUrl[1]); // call
} catch (IOException e) {
return "Unable to retrieve data. URL may be invalid.";
}
}
}
Upvotes: 1
Reputation: 157437
String... sUrl
the three consecutive dots meaning more then one String. The dots are varargs
And how to pass the Context?
you can force it adding a constructor that takes the Context as parameter:
private Context mContext;
public void setContext(Context context){
if (context == null) {
throw new IllegalArgumentException("Context can't be null");
}
mContext = context;
}
Upvotes: 7
Reputation: 1728
you can do something like this inside your doInBackground method:
String a = sUrl[0]
String b = sUrl[1]
execute AsyncTask in this way:
new DownloadFile().execute(string1,string2);
the first value : sUrl[0] will be the one passed from string1 and
surl[1] will be the second value passed i.e string2 !
Upvotes: 5
Reputation: 1564
Ypu can create constructor to pass different type parameters and also strings data using String......str
private class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
byte[] byteArray;
public DownloadTask (Context c,byte[] byteArray){
context = c;
byteArray=byteArray;
}
@Override
protected String doInBackground(String... str) {
{
// use context here
System.out.println(param[0]);
}
}
new DownloadTask(context,bytearray).excute("xyz");
Upvotes: 1
Reputation: 4411
new DownloadFile().execute(Str1,str2,str3,........);
this is one way to pass more url... if you want send more values in doinbackground method..
public class DownloadFile extends AsyncTask<DataHolders, Integer, String> {
public class DataHolders {
public String url;
public String myval;
}
@Override
protected String doInBackground(DataHolders... params) {
return null;
}
}
you can call the class with
DataHolders mhold = new DataHolders();
new DownloadFile().execute(mhold,mhold2,mhold3,........);
Upvotes: 1
Reputation: 39386
You cannot, for the following reasons :
protected String doInBackground(String... sUrl, String... otherUrl, Context context)
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 to otherUrl
?
Moreover, doInBackground
overrides a method from AsyncTask
. 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 calling execute
.
Upvotes: 3
Reputation: 5258
Yes you can pass more values in constructor
but not in doInBackground
Try this way
new DownloadFile(String sUrl,String other Url,Context context).execute();
Async Task
private class DownloadFile extends AsyncTask<String, Integer, String> {
public DownloadFile(String url,String url2,Context ctx)
{
}
@Override
protected String doInBackground(String... sUrl) {
{
}
}
Upvotes: 3
Reputation: 1576
you can use constructor
private class DownloadFile extends AsyncTask<String, Integer, String> {
private Context context;
public void DownloadFile(Context c,String one, int two){
context = c;
}
@Override
protected String doInBackground(String... sUrl) {
{
// use context here
}
}
Upvotes: 1
Reputation: 54672
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
private class DownloadFile extends AsyncTask<String, Integer, String> {
private Context context;
public void setContext(Context c){
context = c;
}
@Override
protected String doInBackground(String... sUrl) {
{
// use context here
}
}
Option 2
Or you can use constructor to pass the values like
private class DownloadFile extends AsyncTask<String, Integer, String> {
private Context context;
public DownloadFile (Context c){
context = c;
}
@Override
protected String doInBackground(String... sUrl) {
{
// use context here
}
}
Upvotes: 11