在我的 android 应用程序中,Facebook 好友正在加载按钮单击。我使用以下功能来获取 facebook 好友。但如果用户未授予发布权限,我将无法获取 Facebook 好友。在发布后,会话将变为 CLOSED_LOGIN_FAILED权限。如果帖子权限在列表视图中加载了 Facebook 朋友。我的代码有什么问题。请帮助我。提前致谢。
private boolean hasPublishPermission() {
Session session = Session.getActiveSession();
return session != null
&& session.getPermissions().contains("publish_stream");
}
private void getFacebookFriends(final Session session) {
Session msession = session;
if (msession.isOpened()) {
if (!hasPublishPermission()) {
if (progressDlg != null) {
progressDlg.dismiss();
}
msession.requestNewPublishPermissions(new NewPermissionsRequest(
FindFriendsActivity.this, PERMISSIONS));
} else {
if (progressDlg != null) {
progressDlg.dismiss();
}
Request friendRequest = Request.newMyFriendsRequest(msession,
new GraphUserListCallback() {
public void onCompleted(List<GraphUser> users,
Response response) {
fbfrndsDetails.clear();
isfindFriendsFromFB = false;
ArrayList<FriendsFinderRequest> data = new ArrayList<FriendsFinderRequest>();
for (int i = 0; i < users.size(); i++) {
Log.d("name", "" + users.get(i).toString());
FriendsDetails fbfrndDetail = new FriendsDetails();
JSONObject jobj;
try {
jobj = users.get(i)
.getInnerJSONObject();
Log.e("name",
"" + jobj.getString("name"));
Log.e("id", "" + jobj.getString("id"));
JSONObject ob = jobj
.getJSONObject("picture");
JSONObject obj = ob
.getJSONObject("data");
} catch (Exception e1) {
e1.printStackTrace();
}
}
if (progressDlg != null) {
progressDlg.dismiss();
}
}
});
Bundle params = new Bundle();
params.putString("fields", "id,name,picture");
friendRequest.setParameters(params);
friendRequest.executeAsync();
}
} else {
if (progressDlg != null) {
progressDlg.dismiss();
}
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(FindFriendsActivity.this);
if (openRequest != null) {
openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
if (!hasPublishPermission()) {
openRequest.setPermissions(PERMISSIONS);
}
if (session.getState() == SessionState.CLOSED_LOGIN_FAILED) {
Session.setActiveSession(null);
msession = new Session(FindFriendsActivity.this);
Session.setActiveSession(msession);
msession.openForPublish(openRequest);
} else {
session.openForPublish(openRequest);
}
}
}
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
if (isfindFriendsFromFB) {
rootLayout.setVisibility(View.GONE);
progressDlg = ProgressDialog.show(FindFriendsActivity.this, "",
"Please wait");
Request friendRequest = Request.newMyFriendsRequest(session,
new GraphUserListCallback() {
public void onCompleted(List<GraphUser> users,
Response response) {
isfindFriendsFromFB = false;
ArrayList<FriendsFinderRequest> data = new ArrayList<FriendsFinderRequest>();
for (int i = 0; i < users.size(); i++) {
Log.d("name", "" + users.get(i).toString());
FriendsDetails fbfrndDetail = new FriendsDetails();
JSONObject jobj;
try {
jobj = users.get(i)
.getInnerJSONObject();
Log.e("name",
"" + jobj.getString("name"));
Log.e("id", "" + jobj.getString("id"));
JSONObject ob = jobj
.getJSONObject("picture");
JSONObject obj = ob
.getJSONObject("data");
} catch (Exception e1) {
e1.printStackTrace();
}
}
if (progressDlg != null) {
progressDlg.dismiss();
}
}
});
Bundle params = new Bundle();
params.putString("fields", "id,name,picture");
friendRequest.setParameters(params);
friendRequest.executeAsync();
}
} else if (state.isClosed()) {
if (progressDlg != null) {
progressDlg.dismiss();
}
}
}