AsyncTask 的 doInBackground 方法中的变量未更新
我尝试使用以下代码来获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
result
未更新的原因是您在调用AsyncTask
后尝试立即检查它。以下是重新构造它的方法:
调用它:
然后修改
onPostExecute
以使用result
执行您需要的任何操作。The reason
result
is not getting updated is because you're attempting to check it immediately after you call theAsyncTask
.Here's how you could re-structure it:
And to call it:
Then modify
onPostExecute
to do whatever you need to withresult
.如果结果是 int 试试这个:
if result is an int try this: