如何等待完成异步任务打印输出?

时间:2014-07-05 09:44:42

标签: android asynchronous android-fragments task wait

在我的MainActivity中我调用异步任务然后调用Fragment,在这个片段中我必须显示异步任务下载的结果,但是在打印输出之前我必须等待异步任务完成!

- 如何等待TASK完成?

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Log.v(TAG, "OnCreate");

        /* make the API call */
        Intent intent = getIntent();
        session = (Session) intent.getSerializableExtra("session");
        String fqlQuery = "select eid,name,description,start_time, pic_big from event where eid in (SELECT eid FROM event WHERE contains("
                + "'{Trento}'" + ")) order by start_time ASC";
        Bundle params = new Bundle();
        params.putString("q", fqlQuery);

        Request request = new Request(session, "/fql", params, HttpMethod.GET,
                new Request.Callback() {
                    public void onCompleted(Response response) {
                        //Log.i(TAG, "Got results: " + response.toString());
                        try {
                            if (response != null) {

                                final JSONObject json = response.getGraphObject().getInnerJSONObject();
                                JSONArray d = json.getJSONArray("data");
                                int l = (d != null ? d.length() : 0);
                                Log.d("Facebook-Example-events Request",
                                        "d.length(): " + l);

                                for (int i = 0; i < l; i++) {
                                    JSONObject o = d.getJSONObject(i);
                                    String id = o.getString("eid");
                                    String title = o.getString("name");
                                    String description = o.getString("description");
                                    String start_time = o.getString("start_time");
                                    String photo = o.getString("pic_big");

                                    EventsHelper f = new EventsHelper();
                                    f.setId(id);
                                    f.setTitle(title);
                                    f.setDescription(description);
                                    f.setStart_time(start_time);
                                    f.setPhoto(photo);
                                    events.add(f);
                                }
                            }
                        } catch (JSONException e) {
                            Log.w("Facebook-Example", "JSON Error in response");
                        }
                    }
                });
        Request.executeBatchAsync(request);



        Fragment fragment = new Fragment_main();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.content_frame, fragment, "basefragment");
        transaction.addToBackStack(null);
        transaction.commit();

2 个答案:

答案 0 :(得分:0)

创建一个扩展AsyncTask的类并覆盖onPostExecute()函数,以定义在任务完成后UI线程上需要发生的事情。

答案 1 :(得分:0)

要等待asyntask直到完成,你必须使用如下的get(),

 Request.executeBatchAsync(request).get();