Reputation: 3660
I am implement a method to get all friend who already have the application installed.
I follow some post & tutorials, but i'm not able to go in OnCompleted method.
Here my code :
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);
}
}
}
If someone already implemented this king of stuff in xamarin android and have a code sample, this will be very helpfull.
Thanks.
Upvotes: 1
Views: 148
Reputation: 138
I am not seeing where you tell the object 'request' about the OnCompleted handler.
You have to explicitly tell the request object what to call when the OnCompleted event occurs.
That code should be right after you create the request and will look a lot like this little snippet:
request.CompletedHandlers += new System.EventHandler(this.OnCompleted);
but modified to suit the API for GraphRequest...
UPDATE:
Right after I saw your comment, I googled GraphRequest and found this:
As I mentioned in above, you have to tell the object what to call, and how to do that will be specific to the API. So I looked it up.
In your case the way to specify the call back is to pass an object that implements the method 'public void onCompleted()' in as the second argument of the call to GraphRequest.NewMyFriendsRequest().
You are passing in the class instance, which does implement the call back interface, except for the spelling of the onCompleted fuction name (you use 'OnCompleted' - change the O to o) and the use of the async keyword in the OnCompleted() function's signature.
That would make the OnCompleted() function not an exact match for the interface definition.
The interface definition is Here
The Facebook API doc does not suggest using an async function, so I don't know why you have this in the definition.
So, change the call back function's definition to:
public void onCompleted(JSONArray p0, GraphResponse p1)
{
Console.WriteLine(p1.ToString());
.....
I apologize for not being more clear earlier.
Upvotes: 1