gpt4 book ai didi

java - 如何将值传递给非 Activity 类

转载 作者:行者123 更新时间:2023-11-30 03:23:25 24 4
gpt4 key购买 nike

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

这里是调用 Activity 上下文并将其传递给 AsyncTask 构造器:

        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);

最佳答案

要将值传递给 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 子类中的 fragment 将是

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

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

只要记住,如果你想做或编辑任何在 UI 线程上运行的东西,你无法在“doInBackground()”方法中执行此操作,无论是在 preExecute() 还是 postExecute() 上,或者在 Runnable 对象中运行它(在 doInBackground() 方法内),但通过调用 runOnUI(myRunnable);

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

关于java - 如何将值传递给非 Activity 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18705942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com