I am using Facebook SDK for Android to allow users to register and use app. I want to get some basic info about user like user name and email. Anyway, I have problem because I get null pointer exception when trying to get user email.
String email = user.getProperty("email").toString();
I assuming that I get this error because I did not request for email permission. How I can do that?
Here is the full code:
public class RegisterActivity extends ActionBarActivity{
Button btnFacebook;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
getSupportActionBar().hide();
btnFacebook = (Button) findViewById(R.id.btn_facebook);
btnFacebook.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("FACEBOOK", "Prije sesije");
// start Facebook Login
Session.openActiveSession(RegisterActivity.this, true, new Session.StatusCallback() {
// callback when session changes state
@SuppressWarnings("deprecation")
@Override
public void call(Session session, SessionState state,
Exception exception) {
Log.d("FACEBOOK", "U call");
if(session.isOpened()){
Log.d("FACEBOOK", "ako je sesija otvorena");
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
Log.d("FACEBOOK", "onCompleted");
if(user != null){
Log.d("FACEBOOK", "user razlicit od null");
String email = user.getProperty("email").toString();
Toast.makeText(getApplicationContext(), "Ime "+user.getUsername().toString()+ "Email "+ email ,
Toast.LENGTH_SHORT).show();
String userId = user.getId();
//new DownloadImageTask(ivProfile).execute("http://graph.facebook.com/"+userId+"/picture?type=large");
Log.d("ID", userId);
}
Log.d("ID", user.getId());
}
});
}
Log.d("FACEBOOK", "nije u sesisji");
}
});
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
Get the permissions first
private static final List<String> PERMISSIONS = Arrays.asList(
"email","user_location");
The Method for getting the user information
Session.openActiveSession(RegisterActivity.this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state,
Exception exception) {
/*
* Session.getActiveSession().onActivityResult(activity1,64206,
* -1,activity1.getIntent());
*/
if (session.isOpened()) {
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
RegisterActivity.this, PERMISSIONS);
session.requestNewReadPermissions(newPermissionsRequest);
return;
}
// make request to the /me API
Request.newMeRequest(session, new GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
Log.d("",""+user);
try {
name=user.getName();
email=user.getProperty("email").toString();
location=(user.getLocation().getProperty("name").toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).executeAsync();
}
}
});
isSubsetof method
private boolean isSubsetOf(Collection<String> subset,
Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
put this code in onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("Activity result"," "+requestCode+" "+resultCode+" "+data.toString());
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
Put Internet permission in Manifest
<uses-permission android:name="android.permission.INTERNET" />
Declare Facebook Login Activity in manifest
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
hey you can try this way...
session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("id", "name", "picture","installed" )).setCallback(statusCallback));
来源:https://stackoverflow.com/questions/22077043/facebook-sdk-3-7-for-android-request-email-permission