Pass more values ​​to doInBackground AsyncTask Android

How to pass more values ​​to doInBackground

My AsyncTask as follows.

 private class DownloadFile extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... sUrl) { { } } 

Is it possible to somehow pass more values ​​to my protected String DoInBackground

for example: protected String doInBackground(String... sUrl, String... otherUrl, Context context)

And how to execute AsyncTask after? new DownloadFile.execute("","",this) or something?

+8
android
source share
9 answers
 String... sUrl 

three consecutive dots meaning more than one line. points are varargs

And how to convey the context?

you can force it to add a constructor that takes a Context parameter as a parameter:

 private Context mContext; public void setContext(Context context){ if (context == null) { throw new IllegalArgumentException("Context can't be null"); } mContext = context; } 
+7
source share

you can send several parameters as you can send them as varargs. But you must use the same type of parameter. Therefore, to do what you are trying, you can follow any of the following actions.

Option 1

you can use the setter method to set some value of a class member and then use it 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 the constructor to pass 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 } } 
+11
source share

you can do something like this inside your doInBackground method:

 String a = sUrl[0] String b = sUrl[1] 

run AsyncTask as follows:

 new DownloadFile().execute(string1,string2); 

first value: sUrl [0] will be passed from string1 and surl [1] will be the second value passed by ie string2!

+4
source share

Yes you can pass more values ​​to constructor , but not to 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) { { } 

}

+3
source share

You cannot for the following reasons:

 protected String doInBackground(String... sUrl, String... otherUrl, Context context) 

is not a valid method signature. Point designations (Varargs) can only be used as the last parameter of a method. This limitation is due to the fact that otherwise polymorphism will be much more complex. Actually, how does Java know which of your lines goes to sUrl , and which goes to otherUrl ?

In addition, doInBackground overrides the method from AsyncTask . Therefore, you cannot change the signature of the method.

However, you can make these values ​​members of your class and pass DownloadFile in the constructor of your class or add setters to set them before calling execute .

+3
source share

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 } } 
+1
source share
 new DownloadFile().execute(Str1,str2,str3,........); 

this is one way to pass more url ... if you want to send more values ​​to the 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,........); 
+1
source share

Ypu can create a constructor to pass various type parameters as well as string 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"); 
+1
source share
 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."; } } } 
0
source share

All Articles