簡體   English   中英

使用更新的Facebook SDK查詢朋友的生日

[英]Querying friends' birthdays using updated facebook sdk

我正在嘗試使用最新的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)

請幫忙!

出於隱私原因,您無法獲得未授權您的應用的朋友的生日。 Graph API v2.0已刪除所有朋友權限: https : //developers.facebook.com/docs/apps/changelog#v2_0

您只能通過以下API調用來獲得通過user_friendsuser_birthday授權您的應用的朋友的生日: me/friends?fields=name,birthday

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM