尽管Simon Cross 的回答被接受并且是正确的,但我想我会用一个需要做的例子(Android)来加强它。我会尽量保持一般性,只关注这个问题。就我个人而言,我最终将东西存储在数据库中,因此加载很顺利,但这需要一个 CursorAdapter 和 ContentProvider ,这有点超出了这里的范围。
我自己来到这里,然后想,现在怎么办?!
问题
就像user3594351一样,我注意到朋友数据是空白的。我通过使用 FriendPickerFragment 发现了这一点。三个月前有效的方法不再有效。甚至 Facebook 的例子也被打破了。所以我的问题是'如何手动创建 FriendPickerFragment?
什么没有奏效
来自Simon Cross的选项 #1不足以邀请朋友使用该应用程序。Simon Cross还推荐了 Requests Dialog,但它一次只允许五个请求。在任何给定的 Facebook 登录会话期间,请求对话框也会显示相同的朋友。没用处。
什么有效(总结)
选项 #2 需要一些努力。您必须确保满足 Facebook 的新规则:1.) 您是游戏 2.) 您有 Canvas 应用程序(Web Presence) 3.) 您的应用程序已在 Facebook 注册。这一切都在 Facebook 开发者网站的Settings下完成。
为了在我的应用程序中手动模拟好友选择器,我执行了以下操作:
- 创建一个显示两个片段的选项卡活动。每个片段显示一个列表。一个片段用于可用朋友(/me/friends),另一个片段用于可邀请朋友(/me/invitable_friends)。使用相同的片段代码来呈现两个选项卡。
- 创建一个从 Facebook 获取好友数据的 AsyncTask。加载该数据后,将其扔给适配器,该适配器会将值呈现到屏幕上。
细节
异步任务
private class DownloadFacebookFriendsTask extends AsyncTask<FacebookFriend.Type, Boolean, Boolean> {
private final String TAG = DownloadFacebookFriendsTask.class.getSimpleName();
GraphObject graphObject;
ArrayList<FacebookFriend> myList = new ArrayList<FacebookFriend>();
@Override
protected Boolean doInBackground(FacebookFriend.Type... pickType) {
//
// Determine Type
//
String facebookRequest;
if (pickType[0] == FacebookFriend.Type.AVAILABLE) {
facebookRequest = "/me/friends";
} else {
facebookRequest = "/me/invitable_friends";
}
//
// Launch Facebook request and WAIT.
//
new Request(
Session.getActiveSession(),
facebookRequest,
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
FacebookRequestError error = response.getError();
if (error != null && response != null) {
Log.e(TAG, error.toString());
} else {
graphObject = response.getGraphObject();
}
}
}
).executeAndWait();
//
// Process Facebook response
//
//
if (graphObject == null) {
return false;
}
int numberOfRecords = 0;
JSONArray dataArray = (JSONArray) graphObject.getProperty("data");
if (dataArray.length() > 0) {
// Ensure the user has at least one friend ...
for (int i = 0; i < dataArray.length(); i++) {
JSONObject jsonObject = dataArray.optJSONObject(i);
FacebookFriend facebookFriend = new FacebookFriend(jsonObject, pickType[0]);
if (facebookFriend.isValid()) {
numberOfRecords++;
myList.add(facebookFriend);
}
}
}
// Make sure there are records to process
if (numberOfRecords > 0){
return true;
} else {
return false;
}
}
@Override
protected void onProgressUpdate(Boolean... booleans) {
// No need to update this, wait until the whole thread finishes.
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
/*
User the array "myList" to create the adapter which will control showing items in the list.
*/
} else {
Log.i(TAG, "Facebook Thread unable to Get/Parse friend data. Type = " + pickType);
}
}
}
我创建的 FacebookFriend 类
public class FacebookFriend {
String facebookId;
String name;
String pictureUrl;
boolean invitable;
boolean available;
boolean isValid;
public enum Type {AVAILABLE, INVITABLE};
public FacebookFriend(JSONObject jsonObject, Type type) {
//
//Parse the Facebook Data from the JSON object.
//
try {
if (type == Type.INVITABLE) {
//parse /me/invitable_friend
this.facebookId = jsonObject.getString("id");
this.name = jsonObject.getString("name");
// Handle the picture data.
JSONObject pictureJsonObject = jsonObject.getJSONObject("picture").getJSONObject("data");
boolean isSilhouette = pictureJsonObject.getBoolean("is_silhouette");
if (!isSilhouette) {
this.pictureUrl = pictureJsonObject.getString("url");
} else {
this.pictureUrl = "";
}
this.invitable = true;
} else {
// Parse /me/friends
this.facebookId = jsonObject.getString("id");
this.name = jsonObject.getString("name");
this.available = true;
this.pictureUrl = "";
}
isValid = true;
} catch (JSONException e) {
Log.w("#", "Warnings - unable to process Facebook JSON: " + e.getLocalizedMessage());
}
}
}