只需要使用Request.newMyFriendsRequest API 就可以取得好友清單,
範例如下
public void requestFriend(){ Request request = Request.newMyFriendsRequest(Session.getActiveSession(), new Request.GraphUserListCallback() { @Override public void onCompleted(List<GraphUser> users, Response response) { if(users!=null){ String uri = null; List<FriendModel> FriendData= new ArrayList<FacebookFriend.FriendModel>(); Map<String, FriendModel> maps = new HashMap<String, FacebookFriend.FriendModel>(); for(GraphUser user :users){ FriendModel model = new FriendModel(user); FriendData.add(model); maps.put(model.Id, model); } } } }); Bundle params = new Bundle(); params.putString("fields", "id,name,picture"); request.setParameters(params); request.executeAsync(); }
在params中加入的fields代表查詢時會回傳的資料,在取得GraphUser 之後,就是要想辦法取得userId,userPicture了,該怎麼做呢? 看看範例
public static class FriendModel{ private String name; private String Id; private String picture; public FriendModel(GraphUser user){ name = user.getName(); Id = user.getId(); picture = getPicture(user); } public FriendModel(String _name,String _Id,String _picture){ name = _name; Id = _Id; picture = _picture; } public String getName(){ return name; } public String getId(){ return Id; } public String getPicture(){ return picture; } private String getPicture(GraphUser user){ String uri=""; Object o = user.getProperty("picture"); if (o instanceof String) { uri = (String) o; } else if (o instanceof JSONObject) { ItemPicture itemPicture = GraphObject.Factory.create((JSONObject) o).cast(ItemPicture.class); ItemPictureData data = itemPicture.getData(); if (data != null) { uri = data.getUrl(); } } uri = uri.replace("s50x50", "s100x100"); return uri; } } private interface ItemPicture extends GraphObject { ItemPictureData getData(); } // Graph object type to navigate the JSON that sometimes comes back instead of a URL string private interface ItemPictureData extends GraphObject { String getUrl(); }
要圖案的話就必須利用上面的方式取得user的圖案,但是這邊有個Tricky的點,就是我將取得的圖檔轉成了100x100 不過這種方法在某些情況下可能會取不到user的圖。 在取得好友圖像以及ID之後就可以做更多的事情啦,例如tag好友,POST朋友塗鴉牆,invite好友....etc
請問3.17.2版facebook sdk 要怎麼取的好友資料@@
回覆刪除目前只能利用invitable friend 來取得可邀請的好友,並且會有相關的圖片和姓名,不過問題是他回傳的id 是暫時性的token,也就是說不是唯一,所以不能用該id來判斷是否為同一人,如何call那隻api 你可以參考http://tedforum.blogspot.tw/2014/05/android-facebook-v20314sdk.html
刪除您好,同樣的問題,現階段(5/1後)有什麼方法可以正確取得好友的facebook 實際id?
回覆刪除目前我也還沒有看到新的方法可以取得真的fb id ,只能取得app scope id
刪除