1

我是 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;
}
4

1 回答 1

0

并非所有朋友都有生日条目。因此,在直接阅读之前,您需要检查是否有“生日”标签可用。您可以在 Graph Explorer 中测试结果

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

于 2013-02-08T16:44:14.227 回答