1

我想将两个值从活动传递到 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);
4

2 回答 2

1

将值传递给 AsyncTask 子类,您可以:

1-传递他们抛出一个构造函数:

class MyTask extends AsyncTask<Void,Void,Void>{
    MyObject myObject = null;
    public MyTask(MyObject myObject){
        this.myObject = myObject;
    }
//....

2-在 execute() 方法参数中传递它:

// your code on the Main thread which will call the execute() method
// ....
new MyTask().execute(myObject);    // i dont remember the exact name of this method, any way

您的 AsyncTask 子类中的片段将是

class MyTask extends AsyncTask<Void,MyObject,Void>{

@Override
public void doInBackGround(MyObject...params){
    MyObject myObject = params[0];
    // the rest of your code
}

请记住,如果您想执行或编辑在 UI 线程上运行的任何事情,您不能在 preExecute() 或 postExecute() 上的“doInBackground()”方法中执行它,或者运行它在 Runnable 对象中(在 doInBackground() 方法内)但通过调用 runOnUI(myRunnable);

希望这会有所帮助,只是我现在不记得方法名称,只是 CTRL + SPACE 将对您的 IDE 有所帮助:D

于 2013-09-09T21:05:52.667 回答
0

你已经在你的构造函数中这样做了

AsyncTaskWebServiceCaller MyTask = new AsyncTaskWebServiceCaller(
                LoginActivity.this, progressDialog,
                getApplicationContext());

那就是在那里传递值

你也传递了两次上下文LoginActivity.this给你contextactivity所以你不需要使用getApplicationContext(). 建议您永远不要真正使用 getApplicationContext()

编辑:

如果你想要上下文,你所要做的就是

public AsyncTaskWebServiceCaller(Context context,ProgressDialog progressDialog) {
    super();
    this.progressDialog = progressDialog;
    this.context = context;
}

你不需要这个活动

要使用共享偏好,您需要做的就是

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
于 2013-09-09T19:59:17.907 回答