GraphRequest永远不会提高完成
问题描述:
我实现了一种方法来获取已安装应用程序的所有朋友。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和有一个代码示例,这将是非常有益的。
谢谢。
答
我没有看到你告诉对象'请求'关于OnCompleted处理程序。
您必须明确告诉请求对象在发生OnCompleted事件时要调用什么。
该代码应该是你创建的要求,并期待有很多这样的小片段之后:
request.CompletedHandlers += new System.EventHandler(this.OnCompleted);
但被修改,以适应GraphRequest的API ...
更新:我看到您的评论
权后,我用Google搜索GraphRequest,发现这个:
正如我在上面提到的,你必须告诉对象要调用什么,以及如何做到这一点将特定于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());
.....
我的不是更清晰早些时候表示歉意。
感谢您的回答,我之前没有找到此信息。我将在明天测试 – OrcusZ
CompletedHandlers在请求对象中不可用 – OrcusZ
感谢您的解释,我按照您的步骤(重新实现)了界面,现在所有工作都正常。 – OrcusZ