mercredi 25 février 2015

Android Facebook onActivityResult data returning null

I am a complete newbee to Android development and trying to integrate Facebook Login. Following the getting started I have successfully integrated the Facebook login button. The Facebook login button changes to logout. Then logout to login as intended.


Now the problem is at onActivityResult the data I am receiving is null. I have spent hours and searched many Stackoverflow links but none I could understand or find any answer. I am sure I am going wrong somewhere, but I just can't seem to figure out where. Any help will be greatly appreciated.


When I try data.GetDataString, I get null.


When I try



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");
}


In the console it prints



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


I then tried to print individual keys using the following



for (String key: bundle.keySet())
{
Log.d ("myApplication", key + " is a key in the bundle");
}


Then I got this in the console.



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


Now my Facebook code is.



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);

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


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

}

@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);
}


}


Thank you very much in advance. Please let me know if you guys require any additional info.


Aucun commentaire:

Enregistrer un commentaire