AsyncTask 的 doInBackground 方法中的变量未更新

发布于 2024-12-27 21:12:02 字数 662 浏览 0 评论 0原文

我尝试使用以下代码来获取 result 值,但它似乎永远不会更新。我正在名为 startPosting() 的类中检查结果:

public class PostDataThread extends AsyncTask<Void, Void, Void> {

  String [] data;
  Context context;
  int result = 0;

  public int startPosting(int type,String data[], Context c) {
    this.data = data;
    this.context = c;
    this.execute();
    return result;
  }

  @Override
  protected Void doInBackground(Void... params) {
    Connect c = new Connect();
    c.start(Constant.RECEIVED_MESSAGE, data, context);
    result = 444;
    return null;
  }

  protected void onPostExecute(Integer result) {
    //          
  }

}

I am trying to use the following code to obtain the result value, but the it never seems to update. I am checking the result in the class called startPosting():

public class PostDataThread extends AsyncTask<Void, Void, Void> {

  String [] data;
  Context context;
  int result = 0;

  public int startPosting(int type,String data[], Context c) {
    this.data = data;
    this.context = c;
    this.execute();
    return result;
  }

  @Override
  protected Void doInBackground(Void... params) {
    Connect c = new Connect();
    c.start(Constant.RECEIVED_MESSAGE, data, context);
    result = 444;
    return null;
  }

  protected void onPostExecute(Integer result) {
    //          
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动次打次papapa 2025-01-03 21:12:02

result 未更新的原因是您在调用 AsyncTask 后尝试立即检查它。

以下是重新构造它的方法:

public class PostDataThread extends AsyncTask<Void, Void, Integer> {

  String [] data;
  Context context;
  int res = 0;

  public PostDataThread(int type, String data[], Context c) {
    this.data = data;
    this.context = c;
  }

  @Override
  protected Void doInBackground(Void... params) {
    Connect c = new Connect();
    c.start(Constant.RECEIVED_MESSAGE, data, context);
    res = 444;
    return res;
  }

  @Override
  protected void onPostExecute(Integer result) {
    Log.d(TAG, "Result is: " +result);
  }

}

调用它:

PostDataThread p = new PostDataThread(type, data, context);
p.execute();

然后修改 onPostExecute 以使用 result 执行您需要的任何操作。

The reason result is not getting updated is because you're attempting to check it immediately after you call the AsyncTask.

Here's how you could re-structure it:

public class PostDataThread extends AsyncTask<Void, Void, Integer> {

  String [] data;
  Context context;
  int res = 0;

  public PostDataThread(int type, String data[], Context c) {
    this.data = data;
    this.context = c;
  }

  @Override
  protected Void doInBackground(Void... params) {
    Connect c = new Connect();
    c.start(Constant.RECEIVED_MESSAGE, data, context);
    res = 444;
    return res;
  }

  @Override
  protected void onPostExecute(Integer result) {
    Log.d(TAG, "Result is: " +result);
  }

}

And to call it:

PostDataThread p = new PostDataThread(type, data, context);
p.execute();

Then modify onPostExecuteto do whatever you need to with result.

放低过去 2025-01-03 21:12:02

如果结果是 int 试试这个:

public class MyActivity extends Activity{
    private int result = 0

    private class MyTask extends AsyncTask<Integer, Void, Integer>{
         public Integer doInBackground(Integer... arg){
             //Conncet c = new Connect();
             //result=c.start(Constant.RECEIVED_MESSAGE, data, context);
             //commented out for debug purposes
             return 23774
         }

         public void onPostExcecute(Integer res){
             result = res
         }
    }

    public void onResume(){
         super.onResume();
         MyTask mt = new MyTask();
         mt.execute(0);
         Handler h = new Handler(new Handler.Callback(){
              public void handleMessage(Message m){
                  //check result here
                  Log.i("RESULT", result);
              }
         });
         //takes 5 seks to wait
         h.sendEmptyMessageDelayed(0, 5000);
    }

}

if result is an int try this:

public class MyActivity extends Activity{
    private int result = 0

    private class MyTask extends AsyncTask<Integer, Void, Integer>{
         public Integer doInBackground(Integer... arg){
             //Conncet c = new Connect();
             //result=c.start(Constant.RECEIVED_MESSAGE, data, context);
             //commented out for debug purposes
             return 23774
         }

         public void onPostExcecute(Integer res){
             result = res
         }
    }

    public void onResume(){
         super.onResume();
         MyTask mt = new MyTask();
         mt.execute(0);
         Handler h = new Handler(new Handler.Callback(){
              public void handleMessage(Message m){
                  //check result here
                  Log.i("RESULT", result);
              }
         });
         //takes 5 seks to wait
         h.sendEmptyMessageDelayed(0, 5000);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文