Search code examples
androidfacebookandroid-asynctask

Querying friends' birthdays using updated facebook sdk


I am trying to access my friends' birthdays using the latest facebook sdk. Due to latest updates, I am forced to call the api multiple times to accomplish this. Once to fetch my friends and then use their user-id to query their birthdays.

The second query, the inner query to get birthdays, is being skipped altogether.

And I am not sure if I am even doing this right.

Here is my background AsyncTask class which contains the calls :

    /**
     * Background Async Task to Load all friends by making calls the Graph API
     * */
    class LoadAllFriends extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ...
        }

        /**
         * getting All friends and birthdays from api
         * */
        protected String doInBackground(String... args) {


            try
            {
                final AccessToken accessToken = AccessToken.getCurrentAccessToken();
                GraphRequestAsyncTask graphRequestAsyncTask = new GraphRequest(
                        accessToken,
                        "/me/friends",
                        null,
                        HttpMethod.GET,
                        new GraphRequest.Callback() {
                            public void onCompleted(GraphResponse response) {
                                try
                                {
                                    friends = response.getJSONObject().getJSONArray("data");

                                    Log.d("Friends length",String.valueOf(friends.length()));
                                    for (int l=0; l < friends.length(); l++)
                                    {
                                        final HashMap hm = new HashMap<String, Date>();
                                        hm.put("uid", friends.getJSONObject(l).getString("id"));
                                        hm.put("name",friends.getJSONObject(l).getString("name"));

                                        GraphRequestAsyncTask graphRequestAsyncTask = new GraphRequest(
                                                accessToken,
                                                "/"+hm.get("uid"),
                                                null,
                                                HttpMethod.GET,
                                                new GraphRequest.Callback() {
                                                    public void onCompleted(GraphResponse response) {
                                                        try
                                                        {
                                                            JSONArray birthday = response.getJSONObject().getJSONArray("data");
                                                            Log.d("birthday",(String) birthday.getJSONObject(0).get("birthday"));
                                                            hm.put("date", (Date) birthday.getJSONObject(0).get("birthday"));
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        }
                                                    }}).executeAsync();

                                        friendsList.add(hm);
                                    }

                                    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
                                    Calendar cal = Calendar.getInstance();
                                    Date date1 = dateFormat.parse(dateFormat.format(cal.getTime()));
                                    cal.add(Calendar.DATE, 30);
                                    Date date2 = dateFormat.parse(dateFormat.format(cal.getTime()));

                                    Iterator<HashMap<String, Object>> iter = friendsList.iterator();
                                    while (iter.hasNext())
                                    {

                                        HashMap<String, Object> map = iter.next();
                                        Log.d("uid", (String) map.get("uid"));
                                        Log.d("name", (String) map.get("name"));
                                        Log.d("date", (String) map.get("date"));
                                        /*if (date1.compareTo((Date) map.get("date")) < 0 ||
                                                date2.compareTo((Date) map.get("date")) > 0)
                                        {
                                            friendsList.remove(map);
                                        }*/
                                    }

                                }
                                catch (JSONException e)
                                {
                                    e.printStackTrace();
                                }
                                catch (ParseException e)
                                {
                                    e.printStackTrace();
                                }

                            }
                        }
                ).executeAsync();

                if (friendsList.size() > 0)
                {
                    friendsFound = true;
                }
                else
                {
                    friendsFound = false;
                }

            }
            catch(NullPointerException e){
                e.printStackTrace();
            }
            catch(RuntimeException e){
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) 
        {
            // dismiss the dialog after getting all events
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() 
            {
                public void run() 
                {
                    ...
                }
            });

        }

    }

Here :

Log.d("birthday",(String) birthday.getJSONObject(0).get("birthday"));

in the inner api call is not displayed in the terminal. Log output is being displayed for friends.length() and from the iterator and only for uid and name. Log for date throws the following error :

AndroidRuntime: FATAL EXCEPTION: main
Process: com.supre.www.surprise, PID: 18142
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native_inner(Native Method)
at android.util.Log.println_native(Log.java:290)
at android.util.Log.d(Log.java:323)
at com.supre.www.surprise.HomeActivity$LoadAllFriends$1.onCompleted(HomeActivity.java:237)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1368)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

Please Help!


Solution

  • You can´t get the birthdays of friends who did not authorize your App, for privacy reasons. All friend permissions have been removed with v2.0 of the Graph API: https://developers.facebook.com/docs/apps/changelog#v2_0

    You can only get birthdays of friends who authorized your App with user_friends and user_birthday, with the following API call: me/friends?fields=name,birthday