6

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:

// start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {

      // callback when session changes state
      @Override
      public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {

          // make request to the 
            Request.executeMyFriendsRequestAsync(session, new Request.GraphUserListCallback() {

                @Override
                public void onCompleted(List<GraphUser> users, Response response) {
                    //Log.d("AL",""+users.size() + response.toString());
                    for (int i=0;i<users.size();i++){
                        Log.d("AL",""+users.get(i).toString());
                        welcome.setText("Done");
                    }

                }
            });
        }
      }
    });

And this is only returning friends name and id only. Where I have to set permissions to get friends birthday? I'm new in facebook SDK. And using SDK v3.

graph api is returning following json result

{Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[{"id":"1580811776","name":"Jamal Abdul Nasir"},{"id":"1610349118","name":"Ahmed Nawaz"}],"paging":{"next":"https:\/\/graph.facebook.com\/100004790803061\/friends?format=json&access_token=AAABtTr8g5U4BANiJdCiBFxQeg0l1eqYYzmSWVM8G1UlyAhTtUrAsoEZAgU19dECXTE2nw7pHIz8bDb7OJGM4wAwzusOVZAQN8yaiYVsQZDZD&limit=5000&offset=5000&__after_id=1610349118"}}}, error: null, isFromCache:false}

which does not have birthday.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Ahmed Nawaz
  • 1,634
  • 3
  • 21
  • 51
  • The graph api returns a json result. Look up how to get data from json. – SnareChops Dec 21 '12 at 05:48
  • Hmm... I'm not sure. Try looking through the [documentation](https://developers.facebook.com/docs/reference/api/) – SnareChops Dec 21 '12 at 06:18
  • In Graph API explorer i got birthday. https://developers.facebook.com/tools/explorer 514418319?fields=friends.fields(name,birthday,id) Can any Body tell me How to do code above graph api request?? – Ahmed Nawaz Dec 21 '12 at 06:44
  • Did that provide you with what you needed? I am not able to get my birthday with that, but it may just be my account. – SnareChops Dec 21 '12 at 06:48
  • @SnareChops yes I got what I need by using above call. but don't know how to code that call in android.?? Need help in coding as well. – Ahmed Nawaz Dec 21 '12 at 06:56

2 Answers2

13

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.

C Abernathy
  • 5,533
  • 1
  • 22
  • 27
  • Do not forget that if you want to maintain your session state correctly, do not forget about two things: **1.** Add `Session.setActiveSession(session);` after `Session session = new Session(FindFriendsActivity.this);` line and **2.** Override activity's `onActivityResult()` with something like this: `@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Session session = Session.getActiveSession(); if (session != null) { session.onActivityResult(this, requestCode, resultCode, data); } }` – Alex Semeniuk Feb 15 '13 at 12:29
0

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.

SnareChops
  • 13,175
  • 9
  • 69
  • 91
  • @ahmedNawaz did you mean to post code in your comment? If so and if there's more then just a line or two you can add it to your question by editing and write a comment here telling me to look at it. – SnareChops Dec 22 '12 at 15:38