大家好我想把变量传递给AsyncTask
我有这个变量
private static String NAMESPACE = "aaa";
private static String METHOD_NAME = "bbb";
private static String SOAP_ACTION = NAMESPACE + METHOD_NAME ;
private static String URL = "ccc";
我完成了这个任务
public class Login extends AsyncTask<Void, Void, String>
{
ProgressDialog progress;
String response = "";
private ProgressDialog pDialog;
public void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please Wait");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(Void... arg0) {
final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("username", user_name);
request.addProperty("userpass", user_pass);
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
response = result.toString();
}
catch (IOException e)
{
response = "Error In The Operation(1) !!\n Check Internet Connection And TRY AGAIN.";
}
catch (Exception e)
{
response = "Error In The Operation(2) !!\n Check Internet Connection And TRY AGAIN.";
}
return response;
}
@Override
public void onPostExecute(String res)
{
if(!(res.equalsIgnoreCase("")))
{
if (res.toString().contains(",") == true)
{
String[] separated = res.split(",");
tv.setText(separated[1]);
return;
}
if(res.toString().equals("1"))
{
res = "Wrong User name OR password ,, TRY AGAIN ..";
tv.setText(res);
pDialog.dismiss();
return;
}
if(res.toString().equals("2"))
{
res = "Your Account Is temporarily Blocked ,, Please Call The Admin";
tv.setText(res);
pDialog.dismiss();
return;
}
if(res.toString().equals("3"))
{
res = "Error While Retrieve S Information ,, Try Again Later .";
tv.setText(res);
pDialog.dismiss();
return;
}
tv.setText(res);
pDialog.dismiss();
}
}
}
我想要执行此Taks时需要
调用它并传递以上变量
像
new Login().execute();
制作
new Login().execute(URL,NAMESPACE,METHOD,USERNAME,USERPASS);
使用Knolledge此任务返回一个字符串:)
AND doInBackground 必须有user_name&amp;的值。 user_pass需要通过执行调用传递它..
问候......
答案 0 :(得分:3)
为什么不为Login类创建一个Constructor,如下所示?在我的例子中,我将一个Activity传递给我的AsyncTask,这样我就可以在完成后调用回调函数,但在你的情况下,你也可以传递一个你的字符串数组。
在下面的这种情况下,args数组被传递给类构造函数,而params数组被传递给doInBackground函数。 MainActivity传递给AsyncTask,以便在任务完成后可以在MainActivity中调用taskDone回调。
public class Login extends AsyncTask<String, Void, String>
{
private MainActivity activity;
//These private strings are only needed if you require them
//outside of the doInBackground function....
//If not, just use the params argument of doInBackground by itself
private String METHODNAME,
private String NAMESPACE;
private String SOAPACTION;
private String USER_NAME;
private String USER_PASS;
public Login(String[] args, MainActivity activity) {
this.NAMESPACE= args[0];
this.METHODNAME = args[1];
this.SOAPACTION = args[2];
this.USER_NAME = args[3];
this.USER_PASS= args[4];
this.activity = activity;
}
@Override
protected Void doInBackground(String... params) {
//Again, use either params local to this function
//or args local to the entire function...
//both would be redundant
String _NAMESPACE = params[0];
String _METHODNAME = params[1];
String _SOAPACTION = params[2];
String _USER_NAME = params[3];
String _USER_PASS= params[4];
//Do background stuff
}
protected void onPostExecute() {
//dismiss progress dialog if needed
//Callback function in MainActivity to indicate task is done
activity.taskDone("some string");
}
}
MainActivity.java
private String[] args= {"mynamespace", "mymethods", "mysoap", "myuser", "mypass"}; //to pass to constructor
private String[] params= {"mynamespace", "mymethods", "mysoap", "myuser", "mypass"}; //to pass to doInBackground
//Pass your args array and the current activity to the AsyncTask
new Login(args, MainActivity.this).execute(params);
//Callback for AsyncTask to call when its completed
public void taskDone(String returnVal) {
//Do stuff once data has been loaded
returnText = returnVal;
}
答案 1 :(得分:2)
让类登录延伸AsyncTask<String, Void,String>
并将doInBackground(Void... params)
更改为doInBackground(String... params)
。
现在,您可以按照所需的方式new Login().execute(URL,NAMESPACE,METHOD,USERNAME,USERPASS);
执行任务,并通过params []数组访问给定的参数。
这意味着你的例子:params [0] == URL,params [1] == NAMESPACE等等。
答案 2 :(得分:1)
首先,你需要改变
public class Login extends AsyncTask<Void, Void, String>
到
public class Login extends AsyncTask<String, Void, String>
并更改
doInBackground(Void...
到
doInBackground(String...
<强> Here is some very helpful documentation on it 即可。如果您仍然遇到问题,请更具体地了解它是什么或不起作用。
异步任务使用的三种类型如下:
Params,执行时发送给任务的参数类型。
进展,期间发布的进度单位的类型 背景计算。
结果,结果的类型 背景计算。
这些是<String, Void, String>
中的参数。第一个String
是正在传递的内容,这就是为什么在doInBackground()
中String...
表示传递的字符串数组
答案 3 :(得分:1)
你可以在执行中传递尽可能多的参数,因为doInbackground(Params ... params)(将其视为Params [] params)只要它们属于同一类型就可以接受任意数量的参数。
但是如果您的参数属于不同的类型(这不是您的情况),您需要将它们作为asynctask类的属性,并通过asynctask构造函数将其值传递为new login(type1 attr1, type2 attr2).execute(params)
: