2016-01-24 40 views
0

我想使用最新的Facebook sdk訪問我的朋友的生日。由於最新更新,我不得不多次調用api來完成此操作。一旦取回我的朋友,然後用他們的user-id查詢他們的生日。使用更新的Facebook查詢朋友的生日sdk

第二個查詢,即獲取生日的內部查詢,將被完全跳過。

而且我不確定我是否在做這件事。

這裏是我的背景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.getJSONObject(0)不用彷徨(「生日」));

內部的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) 

請幫助!

+0

有人請回復! –

回答

2

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

你只能得到誰與user_friendsuser_birthday授權你的App的朋友生日,用下面的API調用:me/friends?fields=name,birthday