Android: How to get friends birth dates from facebook calendar using Graph API

后端 未结 2 588
太阳男子
太阳男子 2020-12-19 07:45

I\'m working on application which need birth dates of all friends. Can some body share code how to get birth dates of all friends using graph API. I\'m using following code:

相关标签:
2条回答
  • 2020-12-19 08:04

    First define your callback, where you'll get friends and birthday info, if authenticated:

    Session.StatusCallback statusCallback =  new Session.StatusCallback() {
    
      // callback when session changes state
      @Override
      public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {
            // Private method, to be defined
            makeFriendsRequest();
        }
      }
    
    };
    

    Then, you can open the session and pass in the necessary, "friends_birthday" permissions request:

    Session session = new Session(this);
    session.openForRead(new Session.OpenRequest(this)
                           .setCallback(statusCallback)
                           .setPermissions(Arrays.asList("friends_birthday")));
    

    Finally, here's the post-authentication method you can use to get friends info, including the birthday:

    private void makeFriendsRequest() {
        Request myFriendsRequest = Request.newMyFriendsRequest(Session.getActiveSession(), 
                new Request.GraphUserListCallback() {
    
            @Override
            public void onCompleted(List<GraphUser> users, Response response) {
                if (response.getError() == null) {
                    // Handle response
                }
    
            }
    
        });
        // Add birthday to the list of info to get.
        Bundle requestParams = myFriendsRequest.getParameters();
        requestParams.putString("fields", "name,birthday");
        myFriendsRequest.setParameters(requestParams);
        myFriendsRequest.executeAsync();
    }
    

    Also check out the SessionLoginSample app, bundled with the SDK if you're looking for different ways you can implement Facebook Login.

    0 讨论(0)
  • 2020-12-19 08:26

    I suppose try using this:

    URL url = "http://YOUR_QUERY_URL_WITH_PARAMS";
    InputStream is = null;
    URLConnection connection = url.openConnection();
    String line;
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(                        connection.getInputStream()));
    while ((line = reader.readLine()) != null) 
    {
        builder.append(line);
    }
    String jSONString = builder.toString();
    JSONObject jSONObject = new JSONObject(jSONString);
    

    Then take the data received and display it.

    Note: I have not tested this code, do not have android device to test on anymore, but should get you pointed in the right direction.

    0 讨论(0)
提交回复
热议问题