first create object and veriable for facebook:
private static String FACEBOOK_APP_ID = "492429660800628";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
after OnCreate Method :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
facebook = new Facebook(FACEBOOK_APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
loginFacebook();//this method when called when you required..
}
private void loginFacebook() {
if (!facebook.isSessionValid()) {
facebook.authorize(this, new String[] { "email", "publish_stream",
"read_stream" }, new LoginDialogListener());
} else {
getProfileInformation();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
try {
getProfileInformation();
} catch (Exception error) {
Toast.makeText(LoginActivity.this, error.toString(),
Toast.LENGTH_SHORT).show();
}
}
public void onFacebookError(FacebookError error) {
Toast.makeText(LoginActivity.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
public void onError(DialogError error) {
Toast.makeText(LoginActivity.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
public void onCancel() {
Toast.makeText(LoginActivity.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
}
please try this method after login facebook:
public void getProfileInformation() {
try {
JSONObject profile = Util.parseJson(facebook.request("me"));
Log.e("Profile", "" + profile);
mUserId = profile.getString("id");
mUserToken = facebook.getAccessToken();
mUserName = profile.getString("name");
mUserEmail = profile.getString("email");
runOnUiThread(new Runnable() {
public void run() {
Log.e("FaceBook_Profile",""+mUserId+"\n"+mUserToken+"\n"+mUserName+"\n"+mUserEmail);
Toast.makeText(getApplicationContext(),
"Name: " + mUserName + "\nEmail: " + mUserEmail,
Toast.LENGTH_LONG).show();
}
});
} catch (FacebookError e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
using latest facebook android SDK from developer.facebook linkhttps://developers.facebook.com/docs/android/fetch-user-data/