Android: how to get friends' birthdays from facebook calendar using Graph API

I am working on an application that needs the birth dates of all friends. Can some code use a common code, how to get the birthdays of all friends using the graphical API. I am using the 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 only returns only the name and identifier of friends. Where do I need to set permissions to get friends for my birthday? I am new to facebook SDK. And using the SDK v3.

api chart returns next 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} 

who has no birthday.

+6
source share
2 answers

First define your callback where you will get friends and birthday information if they are 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 a session and pass the necessary request to "friends_birthday":

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

Finally, here is a post-authentication method that you can use to get information about friends, including your 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 are looking for various ways to implement Facebook login.

+13
source

I guess 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 and show it.

Note: I have not tested this code, you no longer have an Android device, but you must point it in the right direction.

0
source

All Articles