簡體   English   中英

使用最新的Facebook SDK3.0無法獲取FriendList信息

[英]Unable to Get FriendList information using latest Facebook SDK3.0

我是Android上Facebook sdk開發的新手,並且正在進行Graph API查詢以從我的Facebook個人資料中檢索FriendList信息。

我正在使用最新的SDK。 SOF wrt old sdk解決方案,但我想使用最新的工具包來實現。

/me/friends的查詢返回一個JSON響應,其中包含我可以輕松解析的好友的nameIDs 但是,當我嘗試通過查詢/me/friends/?fields=birthday來獲取諸如生日之類的額外信息時,我得到的響應無效。

我也在下面的onCreateView方法中為friends_birthday設置了權限。

這是我的代碼:

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

並非所有朋友都有生日條目。 因此,在直接閱讀之前,您需要檢查是否有“生日”標簽。 您可以在Graph Explorer中測試結果

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM