本文介绍了Facebook API如何等待graphRequest executeAsync完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用脸谱图请求来检索好友列表。但是,如何在调用 graphRequest.executeAsync()之后创建一个函数并返回它?

I am using facebook graph request to retrieve friends list. But, How can I get to create a function and return it after the call graphRequest.executeAsync() is done?.

private Map<String, String> getFacebookFriends(AccessToken accessToken, Profile profile) throws InterruptedException, ExecutionException {
    final Map<String, String> friendsMap = new HashMap<>();
    GraphRequest graphRequest = new GraphRequest(accessToken, "/me/friends", null, HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    JSONObject jGraphObj = response.getJSONObject();
                    try {
                        JSONArray friendsData = jGraphObj.getJSONArray("data");
                        for (int i = 0; i < friendsData.length(); i++) {
                            JSONObject friend = friendsData.getJSONObject(i);
                            String friendId = friend.getString("id");
                            String friendName = friend.getString("name");
                            friendsMap.put(friendId, friendName);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
    );
    List<GraphResponse> gResponseList = graphRequest.executeAsync().get();
    return friendsMap;
}

我目前使用的技术与。通过调用它来像 graphRequest.executeAsync()。get(); 。但它似乎无法正常工作。

I am currently using the same technique like in this post. By calling it like graphRequest.executeAsync().get();. But it seems like it's not working.

上述函数将在 friendsMap > graphRequest 已完成。

The function above will return the friendsMap before the graphRequest is done.

任何建议都表示赞赏。

推荐答案

我通过使用 executeAndWait 代替 executeAsync 函数来完成此工作。所以,这就是最终的结果

I've got this working by using executeAndWait instead of executeAsync function. So, this is how the final looks like

public static Map<String, String> getFacebookFriends(AccessToken accessToken, Profile profile) throws InterruptedException, ExecutionException {
    final Map<String, String> friendsMap = new HashMap<>();

    GraphRequest.Callback gCallback = new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            JSONObject jGraphObj = response.getJSONObject();
            try {
                JSONArray friendsData = jGraphObj.getJSONArray("data");
                for (int i = 0; i < friendsData.length(); i++) {
                    JSONObject friend = friendsData.getJSONObject(i);
                    String friendId = friend.getString("id");
                    String friendName = friend.getString("name");
                    friendsMap.put(friendId, friendName);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    final GraphRequest graphRequest = new GraphRequest(accessToken, "/me/friends", null, HttpMethod.GET, gCallback);
    // Run facebook graphRequest.
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            GraphResponse gResponse = graphRequest.executeAndWait();
        }
    });
    t.start();
    t.join();
    return friendsMap;
}

这篇关于Facebook API如何等待graphRequest executeAsync完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 04:29