In my Android project, I'm using AsyncTask
for Facebook API
news feed request.
I'm getting error: OnPostExecute method executed before doInBackground request finish.
private class NewsFeedAsyncTask extends AsyncTask < Integer, Integer, Boolean > {
@Override
protected Boolean doInBackground(Integer...params) {
if (Session.getActiveSession().getState().isOpened()) {
Get_news_feed();
return true;
}
return false;
}
@Override
protected void onPostExecute(Boolean t) {
super.onPostExecute(t);
adapter = new FacebookAdapter(data, context);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
mPullRefreshListView.onRefreshComplete();
}
}
fetchNewsFeed ()
public void fetchNewsFeed() {
Session.openActiveSessionFromCache(context);
if (Session.getActiveSession().getState().isOpened()) {
Request.executeGraphPathRequestAsync(
Session.getActiveSession(), "me/home", new Request.Callback() {
@Override
public void onCompleted(
Response response) {
}
}
}
}
}
OnActivityCreated()
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new NewsFeedAsyncTask().execute();
}
The onPostExecute
method is always executed after doInBackground
.
Your problem is that the Get_news_feed
method (which is executed in background from the doInBackground
method) posts some stuff to be done back on the UI thread with runOnUiThread
and then returns (before the stuff posted on UI thread is executed).
If you expect the Get_news_feed
to be synchronous (everything done before returning), you should not post anything on the UI thread.
Just do all the UI stuff in onPostExecute
instead.