我正在尝试使用以下代码发布在朋友的墙上,我从他们的一个教程中获取了这些代码。我已经实现了所有生命周期方法,但不清楚发生了什么。
情景:
我有一个与facebook登录相关联的活动和片段 - >我在此设置中请求读取权限,并使用我从Facebook获得的相关信息填充我的数据库。
现在我有一个标签设置,它有不同的MainActivity和子片段,其中两个孩子包含listviews,此列表的每一行都与不同的Facebook好友ID相关联。在这个列表中的facebook_post按钮的clikc上,我使用以下函数在facebook朋友的墙上发布:
if (isNetworkConnected()) {
Session session = Session.getActiveSession();
if (session != null) {
if(isLoggedIn()){
Toast.makeText(getActivity(),"Session not null and logged in", Toast.LENGTH_LONG).show();
if (hasPublishPermission()) {
System.out.println("Has publish");
Toast.makeText(getActivity(),"Session not null and logged in and publish too", Toast.LENGTH_LONG).show();
Bundle params = new Bundle();
params.putString("name", "Digital");
params.putString("link", "http://www.digitals.com/");
params.putString("picture","http://www.exar.com/images/frontend/logo.png");
params.putString("to", FACEBOOK_ID);
params.putString("caption", "Congrulations!");
params.putString("description", textGreeting);
params.putString("message", "message");
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
getActivity(), Session.openActiveSessionFromCache(getActivity()),
params)).setOnCompleteListener(
new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
// frag3.setFbId(null);
// ---------------------------- got to put
// check here
// onBackPressed();
}
}).build();
feedDialog.show();
} else if (session.isOpened()) {
// We need to get new permissions, then complete the action
if (!hasPublishPermission())
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(getActivity(), PERMISSION));
Bundle params = new Bundle();
params.putString("name", "Digital");
params.putString("link", "http://www.digitals.com/");
params.putString("picture","http://www.exareceipts.com/images/frontend/logo.png");
params.putString("to", FACEBOOK_ID);
params.putString("caption", "Congrulations!");
params.putString("description", textGreeting);
params.putString("message", "message");
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
getActivity(), Session.getActiveSession(),
params)).setOnCompleteListener(
new OnCompleteListener() {
@Override
public void onComplete(Bundle values,FacebookException error) {
// frag3.setFbId(null);
// ---------------------------- got to put
// check here
//onBackPressed();
}
}).build();
feedDialog.show();
}
}else{
Toast.makeText(getActivity(),"Please check if you are logged in", Toast.LENGTH_LONG).show();
mainFrag.setParam(1);
Intent mainIntent;
mainIntent = new Intent(getActivity(),FacebookMain.class);
getActivity().startActivity(mainIntent);
}
}
} else {
Toast.makeText(getActivity(),"Please check your internet connection", Toast.LENGTH_LONG).show();
}
}catch (Exception e){
System.out.println("Exception post fb: " +e);
Toast.makeText(getActivity(), "first construct"+e, Toast.LENGTH_SHORT).show();
}
这是我的isLoggedIn函数:
public boolean isLoggedIn() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
return true;
} else {
return false;
}
}
这些是我的LifeCycle方法(在当前片段中):
private final String PENDING_ACTION_BUNDLE_KEY = "com.exa.dig:PendingAction";
private PendingAction pendingAction = PendingAction.NONE;
private GraphUser user1;
private enum PendingAction {
NONE, POST_PHOTO, POST_STATUS_UPDATE
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
public static boolean isActive() {
Session session = Session.getActiveSession();
if (session == null) {
return false;
}
return session.isOpened();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (pendingAction != PendingAction.NONE && (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException)) {
new AlertDialog.Builder(getActivity()) .setTitle("cancelled").setMessage("NotGranted").setPositiveButton("Ok", null).show();
pendingAction = PendingAction.NONE;
} else if (state == SessionState.OPENED_TOKEN_UPDATED) {
handlePendingAction();
}
updateUI();
}
public boolean isLoggedIn() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
return true;
} else {
return false;
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private void updateUI() {
Session session = Session.getActiveSession();
boolean enableButtons = (session != null && session.isOpened());
if (enableButtons && user1 != null) {
// profilePictureView.setProfileId(user.getId());
// greeting.setText(getString(R.string.app_name, user.getFirstName()));
} else {
// profilePictureView.setProfileId(null);
// greeting.setText(null);
}
}
@SuppressWarnings("incomplete-switch")
private void handlePendingAction() {
PendingAction previouslyPendingAction = pendingAction;
// These actions may re-set pendingAction if they are still pending, but
// we assume they
// will succeed.
pendingAction = PendingAction.NONE;
}
private boolean hasPublishPermission() {
Session session = Session.getActiveSession();
return session != null && session.getPermissions().contains("publish_actions");
}
onCreate中的:
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
//===============================================
Session.openActiveSessionFromCache(getActivity());
//================================================
但是我得到了一个例外:
java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request
当我从手机上卸载FB应用程序以检查网络对话框是否正常工作时,通常会发生上述情况。任何的想法?
答案 0 :(得分:1)
错误通常意味着您正在进行权限请求,而另一个正在进行中。如果没有看到代码的全部流程,就无法确定它在哪里发生。
好消息是,由于您只使用了Feed对话框,因此您不需要发布权限。