2016-03-10 102 views
0

我正在使用Facebook圖形請求來檢索朋友列表。但是,如何在電話graphRequest.executeAsync()完成後創建函數並將其返回?Facebook API如何等待graphRequest executeAsync完成

private Map<String, String> getFacebookFriends(AccessToken accessToken, Profile profile) throws InterruptedException, ExecutionException { 
    final Map<String, String> 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<GraphResponse> gResponseList = graphRequest.executeAsync().get(); 
    return friendsMap; 
} 

我目前使用的技術與this post相同。通過稱它爲graphRequest.executeAsync().get();。但它似乎不起作用。

上述功能將在graphRequest完成之前返回friendsMap

任何建議表示讚賞。

回答

2

我通過使用executeAndWait而不是executeAsync函數得到了這個工作。所以,這就是最終看起來像

public static Map<String, String> getFacebookFriends(AccessToken accessToken, Profile profile) throws InterruptedException, ExecutionException { 
    final Map<String, String> 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; 
} 
+0

這幫了我極大。謝謝。 – Iorek

0

對於任何需要在C#中的版本。這是我在我的主要活動中所擁有的。

public static ICallbackManager CallbackManager = CallbackManagerFactory.Create(); 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     var facebookCallback = new FacebookCallback<LoginResult> 
     { 
      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<FbResponse>(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<TResult> : Java.Lang.Object, IFacebookCallback where TResult : Java.Lang.Object 
{ 
    public Action HandleCancel { get; set; } 
    public Action<FacebookException> HandleError { get; set; } 
    public Action<TResult> 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<TResult>()); 
    } 
} 

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<FriendsData> 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; } 
}