1

アクティビティから AsyncTask クラスに 2 つの値を渡し、バックグラウンド プロセスから 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());

そこに値を渡します

また、コンテキストを 2 回LoginActivity.this渡しているため、使用する必要はcontextありactivityません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 に答える