使用更新的facebook sdk查询朋友的生日

时间:2016-01-24 17:02:17

标签: android facebook android-asynctask

我正在尝试使用最新的facebook sdk访问朋友的生日。由于最新的更新,我被迫多次调用api来完成此任务。一旦获取我的朋友,然后使用他们的user-id查询他们的生日。

第二个查询,即获得生日的内部查询,将被完全跳过。

我不确定我是否正确行事。

这是我的背景AsyncTask类,其中包含调用:

    /**
     * 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() 
                {
                    ...
                }
            });

        }

    }

这里:

  

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

内部api呼叫中的

未显示在终端中。正在为friends.length()和迭代器显示日志输出,仅针对uidname。记录date会引发以下错误:

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)

请帮助!

1 个答案:

答案 0 :(得分:2)

出于隐私原因,您无法获得未授权您的应用的朋友的生日。使用图谱API的v2.0删除了所有好友权限:https://developers.facebook.com/docs/apps/changelog#v2_0

您只能通过以下API调用获得使用select maker, MAX(type) from product group by maker, type having count(type)=1 and count(model)>1 user_friends授权您的应用的朋友的生日:user_birthday