我想将两个值从活动传递到 AsyncTask 类,并将它们从后台进程发送到 SOAP Web 服务,但它返回我的 null 或错误,我确信在将值从 LoginActivity 传递到 AsyncTask 时有些错误。
这是我的 LoginActivity 代码:
final EditText LoginId = (EditText) findViewById(R.id.IDLogin);
final EditText LoginPass = (EditText) findViewById(R.id.LoginPass);
contextOfApplication = getApplicationContext();
mPrefs = getSharedPreferences(PREFS, 0);
boolean rememberMe = mPrefs.getBoolean("rememberMe", false);
final String login1 = LoginId.getText().toString();
final String pass1 = LoginPass.getText().toString();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(LoginActivity.this);
prefs.edit().putString("login1", login1).commit();
prefs.edit().putString("password1", pass1).commit();
这是调用并将活动上下文传递给 AsyncTask Constractor :
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialog progressDialog = new ProgressDialog(
LoginActivity.this);
progressDialog.setMessage("جاري تسجيل الدخول الرجاء الانتظار");
progressDialog.show();
AsyncTaskWebServiceCaller MyTask = new AsyncTaskWebServiceCaller(
LoginActivity.this, progressDialog,
getApplicationContext());
MyTask.execute();
}
});
我的完整 AsyncTask 代码:
public class AsyncTaskWebServiceCaller extends AsyncTask<Void, Void, String> {
Activity mActivity;
Context context;
LoginActivity MyClass = new LoginActivity();
public static Context contextOfApplication;
ProgressDialog progressDialog;
Context applicationContext = LoginActivity.getContextOfApplication();
// Constractor
public AsyncTaskWebServiceCaller(Activity activity,
ProgressDialog progressDialog, Context context) {
super();
this.progressDialog = progressDialog;
this.mActivity = activity;
this.context = context;
}
// BackGround Process
@Override
protected String doInBackground(Void... voids) {
// this is executed in a background thread.
// the result is returned to the UI thread via onPostExecute
try {
final String NAMESPACE = "http://ws.sams.com";
final String URL = "http://88.198.82.92:8080/sams1/services/LoginActvityWs?WSDL"; // usint
// //
// localhost
final String METHOD_NAME = "login";
final String SOAP_ACTION = "http://ws.sams.com/login";
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
final HttpTransportSE androidHttpTransport = new HttpTransportSE(
URL);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
String user = prefs.getString("login1", null);
String pass = prefs.getString("password2", null);