问题
The code below is taken from the official tutorial. I'm using ActionBarSherlock, so my Activity extends SherlockFragmentActivity, it works if I use them one time each (login and post), but it can't work if I want to login and post with only one button.
The class variable:
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
The actionbar button:
case R.id.askOnFacebook:
{
final Activity tmpAct = this;
// 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 (session.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(getApplicationContext(), "utente " + user.getName(), Toast.LENGTH_LONG).show();
publishStory(tmpAct);
}
}
});
}
}
});
}
the other methods:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
private void publishStory(final Activity activity) {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", "nam");
postParams.putString("caption", "capProva");
postParams.putString("description", "desAsd");
postParams.putString("link", "https://developers.facebook.com/android");
postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(activity.toString(),
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(activity
.getApplicationContext(),
"ERROR: " + error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(activity
.getApplicationContext(),
"POSTED: " + postId,
Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization);
//XXX !!!!!!! I removed the line below because in the tutorial onCreate method it's not initialized
//uiHelper.onSaveInstanceState(outState);
}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
inside onCreate:
if (savedInstanceState != null) {
pendingPublishReauthorization =
savedInstanceState.getBoolean(PENDING_PUBLISH_KEY, false);
}
I noticed that in debug mode, it never pass from onCompleted, so the publish method is never called. If I move the call to publishStory after Session.openActiveSession it works bad and I get some crash (I suppose because before it need to login and after I need to click again to the button to post to facebook wall).
How all could work with a single click on the button?
来源:https://stackoverflow.com/questions/19666601/android-facebook-sdk-3-login-and-post-to-wall-with-one-button