如何打开com.facebook.Session以获取用户的Facebook好友列表?

时间:2013-07-01 08:22:13

标签: android facebook

我正在开发一款获取用户Facebook好友列表的Android应用。

这是我的代码。

/**
 * 
 * @param session
 * @return
 */
private Request createRequest(Session session)
{
    Request request = Request.newGraphPathRequest(session, "me/friends", null);

    Set<String> fields = new HashSet<String>();
    String[] requiredFields = new String[] { "id", "name", "picture",
            "gender" };
    fields.addAll(Arrays.asList(requiredFields));

    Bundle parameters = request.getParameters();
    parameters.putString("fields", TextUtils.join(",", fields));
    request.setParameters(parameters);

    return request;
}

private void requestMyFacebookFriends(Session session)
{
    Request friendsRequest = createRequest(session);
    friendsRequest.setCallback(new Request.Callback()
    {

        @Override
        public void onCompleted(Response response)
        {
            List<GraphUser> friends = getResults(response);

            mLovers = filterFriendsToMeetUserGenderPref(friends);
            // Set list adapter to show available lovers or friends.
            setListAdapter(new SelectLoversAdapter(SelectLoversActivity.this, R.layout.item_select_lovers, mLovers));
        }
    });
    friendsRequest.executeAsync();
}

private List<GraphUser> getResults(Response response)
{
    GraphMultiResult multiResult = response
            .getGraphObjectAs(GraphMultiResult.class);
    GraphObjectList<GraphObject> data = multiResult.getData();
    return data.castToListOf(GraphUser.class);
}

private Session.StatusCallback statusCallback = new Session.StatusCallback() 
{
    @Override
    public void call(final Session session, SessionState state, Exception exception) 
    {
        if(session.isOpened())
        {
            requestMyFacebookFriends(session);
        }
    }
};

private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback statusCallback)
{
    OpenRequest openRequest = new OpenRequest(activity);
    openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
    openRequest.setCallback(statusCallback);

    Session session = new Session.Builder(activity).build();

    if(SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI)
    {
        Session.setActiveSession(session);
        session.openForRead(openRequest);

        return session;
    }

    return null;
}

这里的主要方法是requestMyFacebookFriends但是我有一个问题:**我不知道如何实例com.facebook.Session

为了得到它,我使用openActiveSession方法,但我不确定我是否必须做我所做的一切,因为我在Facebook开发方面非常新。

我在另一个活动中使用openActiveSession来获取Facebook访问令牌。

我的问题是:我需要做些什么才能打开Session来获取用户的Facebook好友列表?

2 个答案:

答案 0 :(得分:1)

  String fqlQuery = "SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
    Session session = Session.getActiveSession();
    Bundle bundle = new Bundle();
    bundle.putString("q", fqlQuery);

    Request request = new Request(session, "/fql", bundle, HttpMethod.GET, new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            // TODO Auto-generated method stub

            try {

                GraphObject graphObject = response.getGraphObject();

                if(graphObject != null)
                {
                    JSONObject jsonObject = graphObject.getInnerJSONObject();
                    JSONArray jsonArray = jsonObject.getJSONArray("data");

                    for (int i = 0; i < jsonArray.length(); i++) {

                        JSONObject object = jsonArray.getJSONObject(i);
                        String userFriendId = object.getString("uid");
                    }
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }               
        }
    });
    Request.executeBatchAsync(request);

你可以用它来获取用户的朋友列表。希望这就是你所需要的。如果你什么都没有,请告诉我。

答案 1 :(得分:0)

session.openForRead(new Session.OpenRequest(this).setCallback(callback).setPermissions(Arrays.asList("your_permissions")));

link可能会对您有所帮助。

修改

public void getBirthdays(){
        Request fBdays = Request.newMyFriendsRequest(Session.getActiveSession(), new Request.GraphUserListCallback() {

            @Override
            public void onCompleted(List<GraphUser> users, Response response) {
                if(response.getError() == null){
                       for(GraphUser usr:users){
                           Log.d(TAG,usr.getName());
                       }
                }
            }
        });

        Bundle requestParams = fBdays.getParameters();
        requestParams.putString("fields", "name,birthday,picture");//for extra properties
        fBdays.setParameters(requestParams);
        fBdays.executeAsync();
    }