Facebook API如何等待graphRequest executeAsync完成

我正在使用facebook图形请求来检索朋友列表。 但是,如何在调用graphRequest.executeAsync()完成后创建一个函数并返回它?

 private Map getFacebookFriends(AccessToken accessToken, Profile profile) throws InterruptedException, ExecutionException { final Map friendsMap = new HashMap(); GraphRequest graphRequest = new GraphRequest(accessToken, "/me/friends", null, HttpMethod.GET, new GraphRequest.Callback() { public void onCompleted(GraphResponse response) { JSONObject jGraphObj = response.getJSONObject(); try { JSONArray friendsData = jGraphObj.getJSONArray("data"); for (int i = 0; i < friendsData.length(); i++) { JSONObject friend = friendsData.getJSONObject(i); String friendId = friend.getString("id"); String friendName = friend.getString("name"); friendsMap.put(friendId, friendName); } } catch (Exception e) { e.printStackTrace(); } } } ); List gResponseList = graphRequest.executeAsync().get(); return friendsMap; } 

我目前正在使用与此帖相同的技术。 通过调用它来像graphRequest.executeAsync().get(); 。 但它似乎没有用。

上面的函数将在friendsMap之前返回friendsMap

任何建议表示赞赏。

我通过使用executeAndWait而不是executeAsync函数来实现这一点。 所以,这就是决赛的样子

 public static Map getFacebookFriends(AccessToken accessToken, Profile profile) throws InterruptedException, ExecutionException { final Map friendsMap = new HashMap<>(); GraphRequest.Callback gCallback = new GraphRequest.Callback() { public void onCompleted(GraphResponse response) { JSONObject jGraphObj = response.getJSONObject(); try { JSONArray friendsData = jGraphObj.getJSONArray("data"); for (int i = 0; i < friendsData.length(); i++) { JSONObject friend = friendsData.getJSONObject(i); String friendId = friend.getString("id"); String friendName = friend.getString("name"); friendsMap.put(friendId, friendName); } } catch (Exception e) { e.printStackTrace(); } } }; final GraphRequest graphRequest = new GraphRequest(accessToken, "/me/friends", null, HttpMethod.GET, gCallback); // Run facebook graphRequest. Thread t = new Thread(new Runnable() { @Override public void run() { GraphResponse gResponse = graphRequest.executeAndWait(); } }); t.start(); t.join(); return friendsMap; } 

对于任何需要C#版本的人。 这就是我在主要活动中所拥有的。

  public static ICallbackManager CallbackManager = CallbackManagerFactory.Create(); protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); var facebookCallback = new FacebookCallback { HandleSuccess = async shareResult => { Bundle parameters = new Bundle(); parameters.PutString("fields", "id,name,link,email,picture.type(normal),friends"); GraphRequest request = new GraphRequest( shareResult.AccessToken, "/me", parameters, HttpMethod.Get, new OnGraphResulCallback()); await Task.Run(() => { var fbResponse = JsonConvert.DeserializeObject(request.ExecuteAndWait().RawResponse); }); }, HandleCancel = () => { Console.WriteLine("HelloFacebook: Canceled"); }, HandleError = shareError => { Console.WriteLine("HelloFacebook: Error: {0}", shareError); } }; // fb init FacebookSdk.SdkInitialize(this.ApplicationContext); LoginManager.Instance.RegisterCallback(MainActivity.CallbackManager, facebookCallback); Forms.Init(this, bundle); LoadApplication(App.Instance); } 

以下是帮助程序类:

 public class FacebookCallback : Java.Lang.Object, IFacebookCallback where TResult : Java.Lang.Object { public Action HandleCancel { get; set; } public Action HandleError { get; set; } public Action HandleSuccess { get; set; } public void OnCancel() { HandleCancel.Invoke(); } public void OnError(FacebookException error) { HandleError.Invoke(error); } public void OnSuccess(Java.Lang.Object result) { HandleSuccess.Invoke(result.JavaCast()); } } public class FbResponse { public long Id { get; set; } public string Name { get; set; } public Picture Picture { get; set; } public Friends Friends { get; set; } public string Email { get; set; } } public class Friends { public List Data { get; set; } public Paging Paging { get; set; } public Summary Summary { get; set; } } public class Summary { public int Total_Count { get; set; } } public class Paging { public Cursors Cursors { get; set; } } public class Cursors { public string Before { get; set; } public string After { get; set; } } public class FriendsData { public int Id { get; set; } public string Name { get; set; } } public class Picture { public PictureData Data { get; set; } } public class PictureData { public bool Is_Silhouette { get; set; } public string Url { get; set; } }