2014-09-06 41 views

回答

1

首先,您必須在使用Facebook SDK時指定user_birthday權限,然後您可以使用下面的代碼段來請求用戶生日。

修改後的代碼只是給你充分的論證

public void getBirthDate(){ 
    // set permissions 
    List<String> permissions = new ArrayList<String>(); 
    permissions.add("user_birthday"); 

    Session activeSession = Session.getActiveSession(); 
    Context context=getApplicationContext(); 
    if (activeSession == null || activeSession.getState().isClosed()) { 
     Session session = new Session.Builder(context).build(); 
     Session.setActiveSession(session); 
     // set active session 
     activeSession = session; 
    } 
    // if a session is already open then issue a request using the available 
    // session. Otherwise ask for user credentials. 
    if (activeSession.isOpened()) { 
     // User Logged In 
     Session.getActiveSession().closeAndClearTokenInformation(); 

     // Open Active Session 
     Session.openActiveSession(this, true, new Session.StatusCallback() { 
      @Override 
      public void call(final Session session, SessionState state, Exception exception) { 
       if (session.isOpened()) { 
        Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { 
         @Override 
         public void onCompleted(GraphUser user, Response response) { 
          if (user != null) { 
           Log.i(LOGTAG, "Json Data that is coming from FB:" + user.getInnerJSONObject()); 
          } 
         } 
        }); 
       } 
      } 
     }); 
    } else { 
     //If session is not opened 
     OpenRequest openRequest = new Session.OpenRequest((Activity) this); 
     openRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO); 
     //Setting no call back dealing it in OnAcitivityResult 
     openRequest.setCallback(null); 

     //Setting Permission 
     openRequest.setPermissions(permissions); 
     activeSession.openForRead(openRequest); 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (Session.getActiveSession() != null) 
     Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 

    Session activeSession = Session.getActiveSession(); 
    if (activeSession == null || activeSession.getState().isClosed()) { 
     Session session = new Session.Builder(getApplicationContext()).build(); 
     Session.setActiveSession(session); 
     activeSession = session; 
    } 

    if (activeSession.isOpened()) { 
     Session.openActiveSession(this, true, new Session.StatusCallback() { 
      @Override 
      public void call(final Session session, SessionState state, Exception exception) { 
       if (session.isOpened()) { 
        Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { 
         @Override 
         public void onCompleted(GraphUser user, Response response) { 
          if (user != null) { 
           Log.i(LOGTAG, "Json Data that is coming from FB:" + user.getInnerJSONObject()); 
          } 
         } 
        }); 
       } 
      } 
     }); 
    } 
} 

希望這將幫助你

的JSON回調看起來像

{ 
    "id": "146473xxx", 
    "name": "XYZ", 
    "first_name": "dkajsd", 
    "last_name": "kajs", 
    "link": "https://....", 
    "username": "xxxx", 
    "birthday": "12/12/91", 
    "hometown": 
    } 
+0

感謝@dpsingh,我已經試過這但我得到null爲birth_date字段這裏是我的一段代碼if(gUser.getBirthday()!= null)\t \t \t \t \t \t \t user.setUser_birthday(gUser.getBirthday()。toString()); \t \t \t \t \t \t \t別的\t \t \t \t \t user.setUser_birthday( 「」);(user.setLast_name gUser.asMap()獲得( 「姓氏」)的toString()); \t \t \t \t \t \t user.setFb_userid(gUser.asMap().get(「id」)。toString()); \t \t \t \t \t \t Log.e(「user_birthday」,「user_birthday:」+ gUser.getBirthday()); gUser是我的GraphUser對象。 – Harry 2014-09-08 12:25:16

+0

嘗試更新的代碼。 – 2014-09-08 12:46:38

相關問題