GraphRequest 从不在完成时引发
GraphRequest never raise on completed
我正在实现一种方法来获取所有已安装该应用程序的朋友。
我遵循了一些 post 和教程,但我无法进入 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 并且有代码示例,这将非常有帮助。
谢谢。
我没看到你在哪里告诉对象 'request' 关于 OnCompleted 处理程序。
您必须明确告诉请求对象在 OnCompleted 事件发生时调用什么。
该代码应该在您创建请求后立即出现,并且看起来很像这个小片段:
request.CompletedHandlers += new System.EventHandler(this.OnCompleted);
但已修改以适应 GraphRequest 的 API...
更新:
看到你的评论后,我立即用谷歌搜索 GraphRequest 并找到了这个:
正如我在上面提到的,你必须告诉对象调用什么,以及如何调用将特定于 API。所以我查了一下。
在您的情况下,指定回调的方法是将实现方法 'public void onCompleted()' 的对象作为调用的第二个参数传递给 GraphRequest.NewMyFriendsRequest().
您正在传递 class 实例,它确实实现了回调接口,除了 onCompleted 函数名称的拼写(您使用 'OnCompleted' - 将 O 更改为 o) 并在 OnCompleted() 函数的签名中使用 async 关键字。
这将使 OnCompleted() 函数不与接口定义完全匹配。
接口定义为Here
Facebook API 文档不建议使用异步函数,所以我不知道你为什么在定义中有这个。
因此,将回调函数的定义更改为:
public void onCompleted(JSONArray p0, GraphResponse p1)
{
Console.WriteLine(p1.ToString());
.....
抱歉之前没有说清楚。
我正在实现一种方法来获取所有已安装该应用程序的朋友。
我遵循了一些 post 和教程,但我无法进入 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 并且有代码示例,这将非常有帮助。
谢谢。
我没看到你在哪里告诉对象 'request' 关于 OnCompleted 处理程序。
您必须明确告诉请求对象在 OnCompleted 事件发生时调用什么。
该代码应该在您创建请求后立即出现,并且看起来很像这个小片段:
request.CompletedHandlers += new System.EventHandler(this.OnCompleted);
但已修改以适应 GraphRequest 的 API...
更新:
看到你的评论后,我立即用谷歌搜索 GraphRequest 并找到了这个:
正如我在上面提到的,你必须告诉对象调用什么,以及如何调用将特定于 API。所以我查了一下。
在您的情况下,指定回调的方法是将实现方法 'public void onCompleted()' 的对象作为调用的第二个参数传递给 GraphRequest.NewMyFriendsRequest().
您正在传递 class 实例,它确实实现了回调接口,除了 onCompleted 函数名称的拼写(您使用 'OnCompleted' - 将 O 更改为 o) 并在 OnCompleted() 函数的签名中使用 async 关键字。
这将使 OnCompleted() 函数不与接口定义完全匹配。
接口定义为Here
Facebook API 文档不建议使用异步函数,所以我不知道你为什么在定义中有这个。
因此,将回调函数的定义更改为:
public void onCompleted(JSONArray p0, GraphResponse p1)
{
Console.WriteLine(p1.ToString());
.....
抱歉之前没有说清楚。