2013-11-15 59 views
0

我正在整合Facebook的SDK在我的應用程序。我需要獲取用戶的朋友姓名和生日。我添加了「read_friendlists」,「user_birthday」,「user_friends」,「friends_birthday」。Facebook好友生日

當使用應用程序的管理員Facebook帳戶登錄時,我可以正確地獲取生日清單。但是,當我使用任何其他帳戶登錄時,JSON響應中沒有生日字段。

PS:我已經禁用了沙盒模式。在應用程序儀表板顯示「這個應用程序是活的(可見所有用戶)」

+0

,你可以看到這個帖子太[這裏] [1] [1]:http://stackoverflow.com/questions/14000802/android-getting-friends-birthday-using-graph-api-facebook-sdk-3-0?rq = 1 –

回答

0

爲JSON對象,請放一些代碼讓我們來幫助你。 我personnaly用這個來得到facebook用戶的生日 首先你有設置user_birthday的權限嗎?如果您要訪問user_birthday信息

如果用戶不爲空,通常你可以得到由user.getBirthday()的生日是非常重要的,因爲從我看到你正在使用的新的Facebook SDK

你當使用Facebook authbutton

authButton.setReadPermissions(Arrays.asList("user_location", "user_birthday", "user_likes")); 

,或者你可以重新授權許可

Session.ReauthorizeRequest reauthRequest = new Session.ReauthorizeRequest(this, PERMISSIONS). 
       setRequestCode(REAUTHORIZE_ACTIVITY). 
       setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK); 
session.reauthorizeForPublish(reauthRequest); 

哪裏的,你可以設置例如許可權限是包含您的許可數組

+0

感謝您的回覆...請注意,我想獲取用戶的朋友生日,而不是用戶... – lingareddyk

0

OK試試這個

String[] facebook_permissions = { "user_photos", "friends_birthday", "friends_photos" }; 

========================================================================== 

public class FriendListAdapter extends BaseAdapter implements SectionIndexer { 
private LayoutInflater mInflater; 
private String[] sections; 
private GetProfilePictures picturesGatherer = null; 
FriendsList friendsList; 
Hashtable<Integer, FriendItem> listofshit = null; 

public FriendListAdapter(FriendsList friendsList) { 
    Log.d(LOG_TAG, "FriendListAdapter()"); 
    this.friendsList = friendsList; 
    sections = new String[getCount()]; 
    listofshit = new Hashtable<Integer, FriendItem>(); 
    for (int i = 0; i < getCount(); i++) { 
     try { 
    sections[i] = jsonArray.getJSONObject(i).getString("name").substring(0); 
sections[i] = jsonArray.getJSONObject(i).getString("birthday").substring(1); 
     } catch (JSONException e) { 
      sections[i] = ""; 
      Log.e(LOG_TAG, "getJSONObject: " + e.getMessage()); 
     } 
    } 
    if (picturesGatherer == null) { 
     picturesGatherer = new GetProfilePictures(); 
    } 
    picturesGatherer.setAdapterForListener(this); 
    mInflater = LayoutInflater.from(friendsList.getBaseContext()); 
} 

public int getCount() { 
    Log.d(LOG_TAG, "getCount()"); 
    if (jsonArray == null) 
     return 0; 
    return jsonArray.length(); 
} 

public Object getItem(int position) { 
    Log.d(LOG_TAG, "getItem()"); 
    return listofshit.get(position); 
} 

public long getItemId(int position) { 
    Log.d(LOG_TAG, "getItemId()"); 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    Log.d(LOG_TAG, "getView(" + position + ")"); 

    JSONObject jsonObject = null; 
    try { 
     jsonObject = jsonArray.getJSONObject(position); 
    } catch (JSONException e) { 
     Log.e(LOG_TAG, "getJSONObject: " + e.getMessage()); 
    } 

    FriendItem friendItem; 

    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.single_friend, null); 
     friendItem = new FriendItem(); 

     convertView.setTag(friendItem); 
    } 
    else { 
     friendItem = (FriendItem) convertView.getTag(); 
    } 

    friendItem.friendPicture = (ImageView) convertView.findViewById(R.id.picture_square); 
    friendItem.friendName = (TextView) convertView.findViewById(R.id.name); 
    friendItem.friendDob = (TextView) convertView.findViewById(R.id.dob); 
    friendItem.friendLayout = (RelativeLayout) convertView.findViewById(R.id.friend_item); 

    try { 
     String uid = jsonObject.getString("uid"); 
     String url = jsonObject.getString("pic_square"); 
     friendItem.friendPicture.setImageBitmap(picturesGatherer.getPicture(uid, url)); 
    } catch (JSONException e) { 
     Log.e(LOG_TAG, "getJSONObject: " + e.getMessage()); 
     friendItem.friendName.setText(""); 
     friendItem.friendDob.setText(""); 
    } 

    try { 
     friendItem.friendName.setText(jsonObject.getString("name")); 
     friendItem.friendDob.setText(jsonObject.getString("birthday")); 
    } catch (JSONException e) { 
     Log.e(LOG_TAG, "getJSONObject: " + e.getMessage()); 
     friendItem.friendName.setText(""); 
     friendItem.friendDob.setText(""); 
    } 

    listofshit.put(position, friendItem); 

    return convertView; 
} 

public int getPositionForSection(int position) { 
    return position; 
} 

public int getSectionForPosition(int position) { 
    return position; 
} 

public Object[] getSections() { 
    return sections; 
} 
} 

class FriendItem { 
TextView friendDob; 
int id; 
ImageView friendPicture; 
TextView friendName; 
RelativeLayout friendLayout; 

} 

============================================================================== 

String query = "select name, uid, pic_square, birthday from user where uid in 
(select uid2 from friend where uid1=me()) order by name"; 
+0

,你可以在這裏看到這篇文章[鏈接](http://stackoverflow.com/questions/14000802/android-getting-friends-birthday-using-圖的API-Facebook的SDK-3-0?RQ = 1) –