2013-04-03 111 views
-1

在我的android项目中,我使用AsyncTask来调用Facebook新闻提要的请求。OnPostExecute在doInBackground完成之前运行-AsyncTask

我有AsyncTask问题:在doInBackground请求完成之前执行的OnPostExecute方法。

这里是我的代码:

private class Task extends AsyncTask<Integer,Integer, Boolean>{ 

     @Override 
     protected Boolean doInBackground(Integer... params) { 

      try { 


       if(Session.getActiveSession().getState().isOpened()){ 


       Get_news_feed(); 
       return true; 
       } 

       return false; 

      } 
       catch (NullPointerException e) { 

       return false; 
      } 



     } 


     @Override 
     protected void onPostExecute(Boolean t) { 
      super.onPostExecute(t);  

adapter =new Facebook_adapter(facebook.json.gen.klase.Obavijest.data,context); 
      actualListView.setAdapter(adapter); 
      adapter.notifyDataSetChanged();  
      mPullRefreshListView.onRefreshComplete(); 

     } 

    } 

Get_news_feed()

public void Get_news_feed() { 
     try { 

       Session.openActiveSessionFromCache(context); 
      if (Session.getActiveSession().getState().isOpened()) { 
      context.runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        // TODO Auto-generated method stub 
        Request.executeGraphPathRequestAsync(
          Session.getActiveSession(), "me/home",new Request.Callback() { 
           @Override 
           public void onCompleted(
             Response response) { 
if (response.getGraphObject()!=null){ 


           System.out.println("Json:"+response.getGraphObject().getInnerJSONObject()); 

          }); 
       } 
      }); 
      } 
     } catch (Exception e2) { 
      System.out.println("Error:"+e2); 
     } 
     } 
    } 

OnActivityCreated()

@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onActivityCreated(savedInstanceState); 
     new Task().execute(); 

    } 
+1

什么是OnBackground方法? – fiddler

+0

对不起,我的意思是doInBackground() –

+1

是什么让你觉得'onPostExecute'在'doInBackground'之前被调用? – fiddler

回答

2

onPostExecute方法是总是之后执行doInBackground

你的问题是Get_news_feed方法(这是从doInBackground方法后台执行)张贴一些东西做回来runOnUiThread UI线程,然后返回(执行张贴在UI线程的东西前)。

如果您希望Get_news_feed是同步的(返回之前完成的所有事情),则不应在UI线程上发布任何内容。

只需在onPostExecute中完成所有的UI工作。

+0

我没有得到结论问题的原因,也没有(像往常一样)为downvotes。这是一个很好的问题。也许是本地化的(无论这意味着什么),但它的代码值得仔细研究。格式不正确的代码,但很容易修复。随你。只是我的两分钱。寻找为什么我的'postExecute'根本不运行。 – DSlomer64

相关问题