无法使用最新的Facebook SDK 3.0获取好友列表信息

时间:2013-02-07 18:46:50

标签: android facebook-graph-api

我是android上的facebook sdk开发的新手,我正在制作一个Graph API查询来从我的facebook个人资料中检索FriendList信息。

我正在使用最新的sdk。 SOF w.r.t old sdk上有解决方案,但我想使用最新的套件实现它。

/me/friends的查询返回JSON回复nameIDs朋友的回复,我很容易解析。但是当我通过查询/me/friends/?fields=birthday尝试获取生日等额外信息时,我收到的回复无效。

我也在下面的friends_birthday方法中为onCreateView设置了权限。

这是我的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_main, container, false);
    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
    authButton.setFragment(this);
    authButton.setReadPermissions(Arrays.asList("user_location", "user_birthday", "user_likes","read_friendlists","friends_birthday"));   /*setting permissions*/
    userInfoTextView = (TextView) view.findViewById(R.id.userInfoTextView);
    return view;
}


public void onResume() {
    super.onResume();
    Session session = Session.getActiveSession();
    if (session != null &&
           (session.isOpened() || session.isClosed()) ) {
        onSessionStateChange(session, session.getState(), null);
    }
    uiHelper.onResume();
}


private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
        userInfoTextView.setVisibility(View.VISIBLE);
        Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);
        String graphpath="/me/friends?fields=name,birthday";

        Request.executeGraphPathRequestAsync(session, graphpath, new Request.Callback(){

            @Override
            public void onCompleted(Response response) {
                try {
                        userInfoTextView.setText(buildUserInfoDisplay(response));
                } catch (JSONException e) {

                    Log.d(TAG,"JSON exception");
                    e.printStackTrace();
                }

            }



        });
    }


 else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
        userInfoTextView.setVisibility(View.INVISIBLE);
    }
}



 private String buildUserInfoDisplay(Response response) throws JSONException  {

        if(response.getError()!=null)
        Log.d(TAG, "null reponse returned...");
    GraphObject go  = response.getGraphObject();
    JSONObject  jso = go.getInnerJSONObject();
        JSONArray   arr = jso.getJSONArray( "data" );
        JSONObject json_obj = arr.getJSONObject(2);   /*getting the third friend from the list*/
        String birthday   = json_obj.getString( "birthday"); 
        return birthday;
}

1 个答案:

答案 0 :(得分:0)

并非所有朋友都有生日条目。因此,您需要检查是否有生日'标签在直接读取之前可用。您可以在Graph Explorer

中测试结果

https://developers.facebook.com/tools/explorer?method=GET&path=me%2Ffriends%3Ffields%3Dname%2Cid%2Cbirthday