我正在使用以下功能发布到Facebook。但有时我得到不受支持的操作异常,与权限相关:
这样做的正确方法是什么?这是我的代码:
public void postFB(String id) {
System.out.println("in fb");
if (isNetworkConnected()) {
System.out.println("Connected");
Session session = Session.getActiveSession();
if (session != null) {
if(isLoggedIn()){
System.out.println("session not null");
if (hasPublishPermission()) {
System.out.println("Has publish");
Bundle params = new Bundle();
params.putString("name", " something");
params.putString("link", "http://www.examsample.com/");
params.putString("picture","http://www.exam_logo.png");
params.putString("to", 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
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(
getActivity(), PERMISSION));
Bundle params = new Bundle();
params.putString("name", "Something");
params.putString("link", "http://www.examsample.com/");
params.putString("picture","http://www.exam_logo.png");
params.putString("to", 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();
}
}
这是我的isLoggedIn()函数:
public boolean isLoggedIn() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
return true;
} else {
return false;
}
}
这是堆栈跟踪:
java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request