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

时间:2016-04-13 09:13:13

标签: android facebook facebook-graph-api facebook-android-sdk

美好的一天,

我希望将List<String>中的信息保存在Graph Request中,但是当我返回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;
  }

1 个答案:

答案 0 :(得分:1)

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

您可以更改getFriends方法以GraphRequest.Callback作为第二个参数,并使其不返回任何内容。所以看起来或多或少会像这样:

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
    }
}

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