2016-05-30 107 views
1

我實現了一種方法來獲取已安裝應用程序的所有朋友。GraphRequest永遠不會提高完成

我按照一些帖子&教程,但我無法去在OnCompleted方法。

這裏我的代碼:

public class FacebookHelper : Java.Lang.Object, GraphRequest.IGraphJSONArrayCallback 
    { 
     //Facebook structure 
     struct FbUser 
     { 
      public string Id; 
      public string Name; 
     } 

     private List<Friend> allfriends; 

     public List<Friend> Friends { get; set; } 
/// <summary> 
     /// Link a new users with his friends 
     /// </summary> 
     /// <param name="allfriends">list of friends</param> 
     /// <returns>task to be awaitable method</returns> 
     public void AddFacebookFriends(List<Friend> allfriends) 
     { 

      this.allfriends = allfriends; 
      //create client connexion 
      GraphRequest request = GraphRequest.NewMyFriendsRequest(AccessToken.CurrentAccessToken, this); 
      Bundle parameters = new Bundle(); 
      parameters.PutString("fields","id,name"); 
      request.Parameters = parameters; 

      request.ExecuteAsync(); 

     } 

     public async void OnCompleted(JSONArray p0, GraphResponse p1) 
     { 
      Console.WriteLine(p1.ToString()); 
      //get result from facebook 
      dynamic friends = JsonConverter.GenericJsonConverter(p0); 

      List<FbUser> fbUsers = new List<FbUser>(); 


      //push all facebook friend in a list 
      foreach (var friend in friends["data"].Children()) 
      { 
       FbUser fbUser = new FbUser(); 
       fbUser.Id = friend["id"].ToString().Replace("\"", ""); 
       fbUser.Name = friend["name"].ToString().Replace("\"", ""); 
       fbUsers.Add(fbUser); 
      } 


      if (allfriends.Count > 0) 
      { 

       //get all friends object matching the facebook ids 
       var list = from first in allfriends 
          join second in fbUsers 
          on first.User.Facebook_Id 
          equals second.Id 
          select first; 

       //get the friendID of the current user 
       var myFriendId = allfriends.FirstOrDefault(f => f.User.Id == User.Instance.Id).Id; 

       List<UserFriends> userFriends = new List<UserFriends>(); 

       foreach (var item in list) 
       { 
        //Link the current user with this friend 
        UserFriends userFriend = new UserFriends(); 
        userFriend.FriendID = item.Id; 
        userFriend.UserID = User.Instance.Id; 

        userFriends.Add(userFriend); 

        //Link this friend to the current user 
        userFriend = new UserFriends(); 
        userFriend.FriendID = myFriendId; 
        userFriend.UserID = item.User.Id; 

        userFriends.Add(userFriend); 

       } 

       var playingfriends = await ManageFriends.CreateFriend(userFriends); 

       Friends = Friend.Instance.CreateListFriend(playingfriends); 

       await ManageFriends.CreateFriend(userFriends); 
      } 
     } 
    } 

如果有人已經實現了這個王的東西在xamarin Android和有一個代碼示例,這將是非常有益的。

謝謝。

回答

1

我沒有看到你告訴對象'請求'關於OnCompleted處理程序。

您必須明確告訴請求對象在發生OnCompleted事件時要調用什麼。

該代碼應該是你創建的要求,並期待有很多這樣的小片段之後:

request.CompletedHandlers += new System.EventHandler(this.OnCompleted); 

但被修改,以適應GraphRequest的API ...

更新:我看到您的評論

權後,我用Google搜索GraphRequest,發現這個: enter image description here

正如我在上面提到的,你必須告訴對象要調用什麼,以及如何做到這一點將特定於API。所以我查了一下。

在你的情況來指定該呼叫回來的路上是通過實現該方法「公共無效onCompleted()」在作爲呼叫的第二個參數GraphRequest.NewMyFriendsRequest(一個對象)。

您傳遞的類的實例,它確實實現了回調接口,除了的拼寫onCompleted機能的研究名稱(您使用「OnCompleted」 - 改變O至O)和使用OnCompleted()函數的簽名中的async關鍵字。

這會使OnCompleted()函數不是與接口定義完全匹配。

接口定義Here

使用異步功能在Facebook的API文檔並不建議,所以我不知道爲什麼你有這樣的定義。

所以,函數的定義改變呼叫回:

public void onCompleted(JSONArray p0, GraphResponse p1) 
    { 
     Console.WriteLine(p1.ToString()); 
     ..... 

我的不是更清晰早些時候表示歉意。

+0

感謝您的回答,我之前沒有找到此信息。我將在明天測試 – OrcusZ

+0

CompletedHandlers在請求對象中不可用 – OrcusZ

+0

感謝您的解釋,我按照您的步驟(重新實現)了界面,現在所有工作都正常。 – OrcusZ