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) {
//
}
}
The reason
resultis 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
onPostExecuteto do whatever you need to withresult.