2012-01-18 120 views
2

這裏是我用得到Facebook好友列表中的代碼,該類數據返回null,誰能告訴我我在這裏做錯了什麼?

public class FBUser 
{ 
    public long Id { get; set; } 
    public string Username { get; set; } 
    public string Name { get; set; } 
    public string Link { get; set; } 
    public string Picture { get; set; } 

    // empty constructor 
    public FBUser() 
    { } 
} 

列表類

public class FbFriendList 
{ 
    public WcdyList<FBUser> Data { get; set; } 
} 

,這裏是我使用來獲取好友列表的方法:

try 
{ 
    FacebookClient client = new FacebookClient(accesstoken); 
     FbFriendList myfriends = client.Get<FbFriendList>("me/friends?fields=id,name,username,picture,link"); 

     if (myfriends.Data.Count > 0) return myfriends.Data; 
     else return null; 
} 
catch (Exception ex) { } 

myfriends正在返回數據對象null,你能告訴我我在做什麼錯嗎?

+0

你有沒有試圖讓這個信息與[OpenGraph Explorer工具](http://developers.facebook.com/tools/explorer)使用相同的'access_token'? – 2012-01-18 12:12:46

+0

@JuicyScripter yup我使用開放圖形瀏覽器進行了檢查,我得到的結果是,但這裏的問題是Get方法,因爲我沒有得到有關SDK的所有方法的任何文檔,它有一個方法'public T Get (字符串路徑)'我試圖使用 – 2012-01-18 12:22:30

回答

2

你可以用這樣的代碼來獲得朋友的信息

FacebookClient client = new FacebookClient(accesstoken); 
dynamic result = app.Get("me/friends?fields=id,name,username,picture,link"); 

現在你可以映射所產生的數據到您的自定義類...

更新:
使用好像你Data而不是data(你應該堅持原來的域名迴應)

public class FbFriendList { 
    public WcdyList<FBUser> data { get; set; } 
} 

UPDATE2:
您可以遍歷列表得到朋友詳細介紹是這樣的:

foreach (dynamic friend in result.data) { 
    string friendId = friend.id; 
    string friendName = friend.name; 
    // etc... 
} 

或者這樣,如果你還沒有在

var resultData = (IList<object>)result.data; 
foreach (var friendObj in resultData) { 
    var friend = (IDictionary<string, object>)friendObj; 
    string friendId = friend["id"]; 
    string friendName = friend["name"]; 
    // etc... 
} 
+0

我認爲動態只能在.Net 4.0中使用,而我正在使用.Net 3.5 – 2012-01-18 13:48:33

+0

你是對的。對於'.Net'波紋管版本4.0,您可以使用字典var result =(IDictionary )app.Get(「我/朋友?fields = id,名稱,用戶名,圖片,鏈接」) – 2012-01-18 13:54:47

+0

我知道我可以像這樣使用,但我想問,爲什麼當我試圖使用這個'public T獲取(字符串路徑)'時,它不會返回所需的結果,並且請告訴我在填充結果變量後如何閱讀好友列表。 – 2012-01-18 16:27:52

相關問題