在图形请求响应中保存信息

Save information in Graph Request response

美好的一天,

我想在 List<String> 中保存我从 Graph Request 获得的值,但是当我 return list 时,它是空的。谁能帮我看看为什么?

    private List<String> getFriends(AccessToken accessToken) {
     List<String> friendsName = new ArrayList<String>();
    GraphRequest requestt =
            new GraphRequest(
                    accessToken,
                    "/me/taggable_friends",
                    null,
                    HttpMethod.GET,
                    new GraphRequest.Callback() {
                      public void onCompleted(GraphResponse response) {
                        JSONObject obj = response.getJSONObject();
                        JSONArray arr;
                        try {
                          arr = obj.getJSONArray("data");
                            for (int l=0; l < arr.length(); l++) {
                                JSONObject oneByOne = arr.getJSONObject(l);
                                System.out.println(oneByOne.opt("name").toString());
                                friendsName.add(oneByOne.opt("name").toString()); 

                            }
                        } catch (JSONException e) {
                          e.printStackTrace();
                        }
                      }
                    }
            );

    Bundle parameters = new Bundle();
    parameters.putString("fields", "name");
    requestt.setParameters(parameters);
    requestt.executeAsync();
     return friends;
  }

您异步执行请求。这意味着请求不会在 requestt.executeAsync(); 之后立即执行,而是在不久的将来执行。这就是您在构造函数中设置回调的原因。您应该在 onCompleted 回调中处理您的 friendsList 而不是 return 在 getFriends.

中处理它

您可以更改 getFriends 方法以将 GraphRequest.Callback 作为第二个参数并使其 return 为空。所以它看起来或多或少是这样的:

public void getFriends(AccessToken token, GraphRequest.Callback callback) {
    GraphRequest requestt = new GraphRequest(token, "/me/taggable_friends",
                                             null, HttpMethod.GET, callback);
    Bundle parameters = new Bundle();
    parameters.putString("fields", "name");
    requestt.setParameters(parameters);
    requestt.executeAsync();
}

然后你可以这样称呼它:

getFriends(accessToken, new GraphRequest.Callback() {
              public void onCompleted(GraphResponse response) {
                List<String> friendsName = new ArrayList<String>();
                JSONObject obj = response.getJSONObject();
                JSONArray arr;
                try {
                  arr = obj.getJSONArray("data");
                    for (int l=0; l < arr.length(); l++) {
                        JSONObject oneByOne = arr.getJSONObject(l);
                        System.out.println(oneByOne.opt("name").toString());
                        friendsName.add(oneByOne.opt("name").toString()); 
                    }
                    //CONSUME YOUR FRIENDS LIST HERE
                } catch (JSONException e) {
                  e.printStackTrace();
                }
              }
            }
    );

最好将 getFriends 方法重命名为 getFriendsAsync

更好的主意是创建您自己的、更具体的回调。

public interface FacebookFriendsCallback {
    void onFriendsReceived(List<String> friendsNames);
}

然后像那样使用它:

public void getFriends(AccessToken token, FacebookFriendsCallback callback) {
    List<String> friendsName = new ArrayList<String>();
    GraphRequest requestt =
        new GraphRequest(
            accessToken,
            "/me/taggable_friends",
            null,
            HttpMethod.GET,
            new GraphRequest.Callback() {
              public void onCompleted(GraphResponse response) {
                JSONObject obj = response.getJSONObject();
                JSONArray arr;
                try {
                  arr = obj.getJSONArray("data");
                    for (int l=0; l < arr.length(); l++) {
                        JSONObject oneByOne = arr.getJSONObject(l);
                        System.out.println(oneByOne.opt("name").toString());
                        friendsName.add(oneByOne.opt("name").toString()); 

                    }
                    callback.onFriendsReceived(friendsName);
                } catch (JSONException e) {
                  e.printStackTrace();
                }
              }
            }
    );
    Bundle parameters = new Bundle();
    parameters.putString("fields", "name");
    requestt.setParameters(parameters);
    requestt.executeAsync();
}

然后调用getFriends方法:

getFriends(accessToken, new FacebookFriendsCallback() {
    public void onFriendsReceived(List<String> friendsNames) {
        //consume list here
    }
}

注意:这不是经过测试的、可以复制粘贴的代码。而是您自己实施的想法。还需要一些错误处理。