【发布时间】:2014-07-09 14:45:11
【问题描述】:
我集成了 facebook 分享,过去几天它运行良好,但今天在问这个问题前 2 小时我遇到了这个问题。
我正在使用此代码进行登录:
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Toast.makeText(ImagePagerActivity.this, user.getName()+" Logged In...", Toast.LENGTH_LONG).show();
}
}
}).executeAsync();
}
}
});
publishFeedDialog();
问题在于 if() 条件始终为假,我也使用了session.isOpened(),它也返回假,我很困惑为什么会发生这种情况。
我已经在清单中声明了 INTERNET、ACCESSNETWORKSTATE、权限
<application>
..........
..........
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
</application>
请帮忙...
编辑
这是我实现的代码,你可以看到onCreateOptionsMenu,onOptionsItemSelected,onActivityResult,publishFeedDialog
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share1);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share1: // facebook item selected
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();
//start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Toast.makeText(ImagePagerActivity.this, user.getName()+" Logged In...", Toast.LENGTH_LONG).show();
}
}
}).executeAsync();
}
}
});
publishFeedDialog();
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
@Override
public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
Log.e("Activity", String.format("Error: %s", error.toString()));
}
@Override
public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
Log.i("Activity", "Success!");
}
});
}
@Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.getCurrentItem());
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
private void publishFeedDialog() {
Log.v("pos",""+pos);
Bundle params = new Bundle();
params.putString("name", ItemListApplication.names.get(pos).getItem());
params.putString("caption", "Irrational beliefs");
params.putString("description",ItemListApplication.names.get(pos).getDesc() );
params.putString("link", "https://www.facebook.com/vivek.warde.3?ref=tn_tnmn");
if(pos==0) params.putString("picture","http://i.imgur.com/lOIUcW2.jpg");
else params.putString("picture",imageUrls[pos]);
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(this,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(ImagePagerActivity.this,
"Posted story, id: "+postId,
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(ImagePagerActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(ImagePagerActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(ImagePagerActivity.this,
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}
})
.build();
feedDialog.show();
}
【问题讨论】:
-
您在哪一行得到错误?您是否尝试过从缓存
openActiveSessionFromCache打开会话? -
@Pr38y 不,我不知道,顺便说一下,我使用
openActiveSession,正如您在代码中看到的那样 -
请在您创建会话的位置发布代码
-
我编辑了我的问题,你可以看到我的代码..
-
您是否在您的 facebook 开发仪表板中将共享指定为权限之一?