Android Facebook onActivityResult数据返回null

时间:2015-02-25 23:41:50

标签: android facebook android-intent facebook-android-sdk android-facebook

我是Android开发的新手,并试图整合Facebook登录。入门后我成功整合了Facebook登录按钮。 Facebook登录按钮更改为注销。然后注销以按预期登录。

现在问题在于onActivityResult,我收到的数据为空。我花了几个小时搜索了许多Stackoverflow链接,但没有我能理解或找到任何答案。我确信我在某个地方出错了,但我似乎无法弄清楚在哪里。任何帮助将不胜感激。

当我尝试data.GetDataString时,我得到null。

当我尝试

 if(resultCode == RESULT_OK){
        Bundle bundle = data.getExtras();

        String id = bundle.toString();
        System.out.println("I am inside resultcode " +id);
    }else
    {
        System.out.println("Not okay");
    }

在控制台中打印

Data is Bundle[{com.facebook.LoginActivity:Result=com.facebook.AuthorizationClient$Result@42a54038}]

然后我尝试使用以下

打印单个密钥
for (String key: bundle.keySet())
{
  Log.d ("myApplication", key + " is a key in the bundle");
}

然后我在控制台中得到了这个。

com.facebook.LoginActivity:Result is a key in the bundle

现在我的Facebook代码是。

public class SignupActivity extends ActionBarActivity{

private static final String TAG = "Custom debug";
private UiLifecycleHelper uiHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.test.testApp",
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

    setContentView(R.layout.activity_signup);

    LoginButton fbAuthBtn = (LoginButton) findViewById(R.id.fbSignupBtn);
    fbAuthBtn.setReadPermissions(Arrays.asList("public_profile","email"));

    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);



}

@Override
protected void onResume() {
    super.onResume();

    // Logs 'install' and 'app activate' App Events.
    AppEventsLogger.activateApp(this);
}

@Override
protected void onPause() {
    super.onPause();

    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this);
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}


//Facebook methods
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
   uiHelper.onActivityResult(requestCode, resultCode, data);
    //Session.getActiveSession().onActivityResult(SignupActivity.this, requestCode, resultCode, data);



    Log.i(TAG, "Request code is " + requestCode + " ResultCode is " + resultCode + " And the data is " + data.getDataString());

    if(resultCode == RESULT_OK){
        Bundle bundle = data.getExtras();

        String fbData = bundle.toString();
        System.out.println("I am inside resultcode " +fbData);
    }else
    {
        System.out.println("I have no idea what is happening :(");
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

}

非常感谢您提前。如果你们需要任何其他信息,请告诉我。

更新

我终于找到了解决方案。所以我在这里添加解决方案,以便帮助像我这样的其他人最终搜索数小时或节省一些宝贵时间。在上面的代码中,我试图在onActivityResult中打印数据。但这不正确。

uiHelper.onActivityResult(requestCode, resultCode, data);

以上代码调用

 private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

当调用onSessionSateChange时,将上面的内容修改为以下

private void onSessionStateChange(Session session, SessionState sessionState, Exception exception) {
    if (sessionState.isOpened()) {
        Log.i(TAG, "Logged in...");

        Request.newMeRequest(session, new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {

                if(user != null){
                    String firstname = user.getFirstName();
                    String lastname = user.getLastName();
                    String id = user.getId();
                    String email = user.getProperty("email").toString();
                    String dob = user.getBirthday();
                    String username = user.getUsername();

                    Log.i(TAG, "id is " + id + " First name is " + firstname + " Lastname is " + lastname + " and email is " + email );

                }else
                {
                    Log.i(TAG, "Screwed :( ");
                }
            }
        }).executeAsync();

    } else if (sessionState.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

所以从上面开始,如果会话被打开,将调用Facebook的图形API,它将回调数据返回到newMeRequest的onCompleted方法。这是我们最终直接访问数据的地方。 我相信在onCompleted方法中我们可以添加我们的代码以继续进行更多活动。

经过进一步研究,可以直接检索键入的方法,如用户名,id,名字等。应使用getProperty方法检索非类型的方法,如电子邮件,语言环境等。

所以最后上面的代码在控制台上产生了以下结果。 id是123456名字是Blahfirst姓氏是Blahlast,电子邮件是blah@gmail.com

以下是Facebook图表api调用的链接以及有关如何检索数据的完整信息。 https://developers.facebook.com/docs/android/graph#userdata-step1

希望解决方案可以帮助某人。我现在正在进一步发展:)

0 个答案:

没有答案