Pass more values in doInBackground AsyncTask Andro

2020-07-06 07:48发布

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?

标签: android
9条回答
欢心
2楼-- · 2020-07-06 08:13

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
        }
}
查看更多
ゆ 、 Hurt°
3楼-- · 2020-07-06 08:13

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");
查看更多
Emotional °昔
4楼-- · 2020-07-06 08:19
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.";
                }
            }
    }
查看更多
来,给爷笑一个
5楼-- · 2020-07-06 08:20

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.

查看更多
倾城 Initia
6楼-- · 2020-07-06 08:22

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 !

查看更多
小情绪 Triste *
7楼-- · 2020-07-06 08:27
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;
}
查看更多
登录 后发表回答