2012-09-03 77 views
2

我想獲取我朋友列表中的所有生日日期。我已經嘗試了幾天。我的代碼如下。Android從Facebook個人資料中獲取生日日期

Facebook facebook = new Facebook(<MY_FACEBOOK_ID>); 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    mContext = this; 
    setContentView(R.layout.main); 

    showNotification(); 

    final String[] PERMISSIONS = new String[] {"user_location", "user_birthday"};   


    facebook.authorize(this, PERMISSIONS, new DialogListener() { 
     @Override 
     public void onComplete(Bundle values) {} 


     @Override 
     public void onCancel() {} 

     @Override 
     public void onFacebookError(FacebookError e) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onError(DialogError e) { 
      // TODO Auto-generated method stub 

     } 
    }); 

    try { 

      String token = facebook.getAccessToken(); 

      Bundle bundle = new Bundle(); 
      bundle.putString("fields", "id,name"); 
      bundle.putString("access_token", token); 

       String response = facebook.request("me/friends", bundle); 

       facebook.setAccessToken(facebook.getAccessToken()); 

       Toast.makeText(mContext, token , Toast.LENGTH_LONG).show(); 
       Toast.makeText(mContext, response, Toast.LENGTH_LONG).show(); 

       JSONObject json = Util.parseJson(response); 

       JSONArray data = json.getJSONArray("data"); 

       for (int i = 0; i < data.length(); i++) 
       { 
        JSONObject friend = data.getJSONObject(i); 

        String id = friend.getString("id"); 
        String name = friend.getString("name"); 
        Toast.makeText(mContext, "ID: "+ id , Toast.LENGTH_LONG).show(); 
        Toast.makeText(mContext, "NAME: "+ name , Toast.LENGTH_LONG).show(); 
       } 

每次使用此代碼都會給出錯誤。和令牌都顯示爲空,並響應顯示問題 「一個積極的訪問令牌必須用於查詢當前用戶的信息」

+0

的可能欺騙 - http://facebook.stackoverflow.com/questions/4645595/how-to-get-all-friends-name-id-birthday-location-etc-from-facebook-api-in和http://stackoverflow.com/questions/3393610/how-to-get-friends使用Facebook的-API - 生日一覽 – deesarus

回答

5

創建方法:

getProfileInformation() 

在方法體,你必須寫:

public void getProfileInformation() { 
mAsyncRunner.request("me", new RequestListener() { 
    @Override 
    public void onComplete(String response, Object state) { 
     Log.d("Profile", response); 
     String json = response; 
     try { 
      JSONObject profile = new JSONObject(json); 
      // getting name of the user 
      String name = profile.getString("name"); 
      // getting email of the user 
      String email = profile.getString("email"); 
      //getting user birthday 
      String birth_day=profile.getString("birthday"); 

      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show(); 
       } 

      }); 

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

    @Override 
    public void onIOException(IOException e, Object state) { 
    } 

    @Override 
    public void onFileNotFoundException(FileNotFoundException e, 
      Object state) { 
    } 

    @Override 
    public void onMalformedURLException(MalformedURLException e, 
      Object state) { 
    } 

    @Override 
    public void onFacebookError(FacebookError e, Object state) { 
    } 
}); 

}

上述功能會從Facebook獲取JSON數據。您需要解析json才能獲取個人配置文件數據。

來自Facebook的樣品輪廓JSON將是這樣的:

 { 
    "id": "1464730016", 
    "name": "XYZ", 
    "first_name": "XYZ", 
    "last_name": "ABC", 
    "link": "https://....", 
    "username": "XYZ", 
    "birthday": "22/10/89", 
    "hometown": 
} 

詳情可參考:

http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/ 
相關問題