我正在将Android应用程序升级到Facebook sdk 4.0和Graph API v2.0,我想知道Session.StatusCallBack和OpenRequest是否具有等效的类。这是我的代码,如果有人可以帮助的话,那会很棒:))

    OpenRequest op = new Session.OpenRequest(activity);

    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_RAW_RESPONSES);

    op.setCallback(statusCallback);

    List<String> permissions = new ArrayList<String>();
    permissions.add(Constants.EMAIL);
    op.setPermissions(permissions);
    Session sess = new Session.Builder(activity).build();
    Session.setActiveSession(sess);
    sess.openForRead(op.setPermissions(permissions));


和我的StatusCallback

Session.StatusCallback statusCallback = new Session.StatusCallback() {
                                          @Override
                                          public void call(Session session, SessionState state, Exception exception) {
                                              // Request.executeMeRequestAsync(session, graghUserCallback);
                                              if (exception != null) {
                                                  String exMsg = exception.getMessage();
                                                  if (exMsg != null && exMsg.startsWith("ApiException:Error")) {
                                                      isExpireToken = true;
                                                      callbackLogin.onFbLoginError(context.getString(R.string.msgFBConnectFaild));
                                                      String url = Constants.API_FB_TOKEN + myApp.getFbToken();
                                                      Api.sendError(myApp, url, 0, exMsg, Method.POST, null);
                                                  }
                                              }
                                              CommonUsed.showProgressDialog(context.getResources().getString(R.string.waiting), context);
                                              runQueryGetUserProfile(session, ConnectionBase.this.callbackLogin);

                                          }

                                      };

最佳答案

请参阅upgrade guidelogin guide。基本上,您使用LoginManager进行登录,并使用CallbackManager注册回调。

public class MainActivity extends FragmentActivity {
    CallbackManager callbackManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { ... });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    // Somewhere in your on activity to actually do login:
    private void login() {
         LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
    }
    ...
}

10-06 06:25