How to get user profile information from Facebook API Android

前端 未结 6 2065
半阙折子戏
半阙折子戏 2020-12-05 08:53

I\'m trying to get user profile information Facebook. Begins loading, I click to confirm permission to send my profile, again, continue loading but eventually get empty fiel

6条回答
  •  北海茫月
    2020-12-05 09:34

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

提交回复
热议问题