Hi All I want to pass a Variables to AsyncTask
I've This Variables
private static String NAMESPACE = "aaa";
private static String METHOD_NAME = "bbb";
private static String SOAP_ACTION = NAMESPACE + METHOD_NAME ;
private static String URL = "ccc";
and I've This Task
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();
}
}
}
I Need When I Want To Execute this Taks
To Call It And Pass The Above Variables
Like
new Login().execute();
Make It
new Login().execute(URL,NAMESPACE,METHOD,USERNAME,USERPASS);
With Knolledge That this task return a String :)
AND THE doInBackground MUST HAVE a value for user_name & user_pass Need To Pass It With Execution Call ..
Regards ...
Let the class Login extend
AsyncTask<String, Void,String>
and changedoInBackground(Void... params)
todoInBackground(String... params)
. Now you can execute the task in the wanted way,new Login().execute(URL,NAMESPACE,METHOD,USERNAME,USERPASS);
, and access the given parameters via the params[] array. This means for your example: params[0] == URL, params[1] == NAMESPACE and so on.The first thing is you need to change
public class Login extends AsyncTask<Void, Void, String>
to
public class Login extends AsyncTask<String, Void, String>
and change
doInBackground(Void...
to
doInBackground(String...
Here is some very helpful documentation on it. If you are still having problems, be a little more specific as to what is or isn't working about it.
These are the paramaters in
<String, Void, String>
. The firstString
is what is being passed which is why indoInBackground()
you haveString...
that indicates and array of strings being passedWhy not create a Constructor for the Login class, like below? In my case, I'm passing an Activity to my AsyncTask so that I can call a callback function when done, but in your case, you could also pass an Array of your Strings.
In this case below, the args array is passed to the class constructor while the params array is passed to the doInBackground function. The MainActivity is passed to the AsyncTask so that the taskDone callback can be called in MainActivity once the task completes.
MainActivity.java
you can pass as many params as you want in execute because doInbackground(Params... params) (think of it as Params[] params) accepts as many parameters as you want as long as they are of the same type.
but if your parameters are of different types (which isn't your case) you need to have them as attributes for asynctask class and pass their values through your asynctask constructor as
new login(type1 attr1, type2 attr2).execute(params)
: