如何在doInBackground
我的AsyncTask
看起来像这样。
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
{
}
}
是否有可能以某种方式在我的protected String DoInBackground
例如:protected String doInBackground(String... sUrl, String... otherUrl, Context context)
如何execute
之后AsyncTask
? new DownloadFile.execute("","",this)
还是什么?
答案 0 :(得分:11)
您可以发送多个参数,因为您可以将它们作为varargs发送。但是你必须使用相同类型的参数。所以要做你想做的事,你可以遵循以下任何一个
选项1
您可以使用setter方法设置类成员的某些值,然后在doInBackGround中使用它们。例如
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
}
}
选项2
或者你可以使用构造函数来传递像
这样的值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
}
}
答案 1 :(得分:7)
String... sUrl
连续三个点意味着多于一个字符串。 点是varargs
如何传递上下文?
你可以强制它添加一个以Context为参数的构造函数:
private Context mContext;
public void setContext(Context context){
if (context == null) {
throw new IllegalArgumentException("Context can't be null");
}
mContext = context;
}
答案 2 :(得分:4)
你可以在doInBackground方法中做这样的事情:
String a = sUrl[0]
String b = sUrl[1]
以这种方式执行AsyncTask:
new DownloadFile().execute(string1,string2);
第一个值:sUrl [0]将是从string1和
传递的值
surl [1]将是第二个传递的值,即string2!
答案 3 :(得分:3)
是的,您可以在constructor
中传递更多值,但不能在 doInBackground
试试这种方式
new DownloadFile(String sUrl,String other Url,Context context).execute();
异步任务
private class DownloadFile extends AsyncTask<String, Integer, String> {
public DownloadFile(String url,String url2,Context ctx)
{
}
@Override
protected String doInBackground(String... sUrl) {
{
}
}
答案 4 :(得分:3)
您不能,原因如下:
protected String doInBackground(String... sUrl, String... otherUrl, Context context)
不是有效的方法签名。点符号(Varargs)只能用作方法的最后一个参数。这种限制是因为否则会使多态变得更加复杂。事实上,java如何知道哪些字符串转到sUrl
,哪些转到otherUrl
?
此外,doInBackground
会覆盖AsyncTask
的方法。因此,您无法更改方法签名。
但是,您可以做的是让这些值成为您的类的成员并传入DownloadFile
类的构造函数,或者在调用execute
之前添加setter来设置它们。
答案 5 :(得分:1)
你可以使用构造函数
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
}
}
答案 6 :(得分:1)
new DownloadFile().execute(Str1,str2,str3,........);
这是传递更多网址的一种方法...如果你想在doinbackground方法中发送更多的值..
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;
}
}
你可以用
打电话给班级DataHolders mhold = new DataHolders();
new DownloadFile().execute(mhold,mhold2,mhold3,........);
答案 7 :(得分:1)
Ypu可以创建构造函数来传递不同的类型参数,并使用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");
答案 8 :(得分:0)
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.";
}
}
}