Android Facebook get all profile information

后端 未结 4 1264
暗喜
暗喜 2021-02-05 21:17

How i can fetch all of user profile information from facebook (like first name, last name, email etc.)

I have downloaded the FB SDK but there is no example for getting t

4条回答
  •  名媛妹妹
    2021-02-05 21:58

    First You create your application and get application id then pass this id to your code like this. Initialize globally

    public static final String mAPP_ID = "Your APP ID";
    Facebook mFacebook= new Facebook(mAPP_ID); 
    

    and setOnClickListener on your button in On Create()

    // facebook login button click event
    try{            
        //mFacebook.logout(LoginActivity.this);
        ((Button)findViewById(R.id.loginPageFaceBookButton)).setOnClickListener(loginButtonListener);
    
        SessionStore.restore(mFacebook,LoginPage.this);         
    } catch (Exception e) {
            Toast.makeText( LoginPage.this,"Exception"+e.toString(), Toast.LENGTH_SHORT).show();
    }
    
    
        // loginButtonListener
            //----------------------------------------------
    
            private OnClickListener loginButtonListener = new OnClickListener()
            {
                public void onClick( View v )
                {
                    if(!mFacebook.isSessionValid() )
                    {   
                        mFacebook.authorize(LoginPage.this, new String[] {"publish_stream","email","user_groups","read_stream","user_about_me","offline_access"},Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
                    }
                    else
                    {
                        try 
                        {
    
                            JSONObject json = Util.parseJson(mFacebook.request("me"));
                            facebookID = json.getString("id");
                            facebookEmail = json.getString("email");
                            faceBooklastName=json.getString("last_name");
                            faceBookFirstName=json.getString("first_name");
    
    
                        }
                        catch (Exception e)
                        {
                            // TODO: handle exception
                            //Toast.makeText( LoginActivity.this,"Exception FB "+e.toString(), Toast.LENGTH_SHORT).show();
                        }
    
                        catch( FacebookError error )
                        {
                            Toast.makeText( LoginPage.this,error.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }; 
    
    
    //onActivityResult
        //***********************************************************
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
    
            mFacebook.authorizeCallback(requestCode, resultCode, data);
        }
    
    
        // DialogListener CLASS STATRT HERE.
    
         public final class LoginDialogListener implements DialogListener
            {
                public void onComplete(Bundle values)
                {
                    try 
                    {
                        JSONObject json = Util.parseJson(mFacebook.request("me"));
                        facebookID = json.getString("id");
                        facebookEmail = json.getString("email");
                        SessionStore.save(mFacebook, LoginPage.this); Toast.makeText( LoginPage.this,"facebookID :"+facebookID+" \n "+"facebookEmail : "+facebookEmail, Toast.LENGTH_SHORT).show();
                    }
                    catch( Exception error ) 
                    {
                        Toast.makeText( LoginPage.this, error.toString(), Toast.LENGTH_SHORT).show();
                    }
                    catch( FacebookError error )
                    {
                        Toast.makeText( LoginPage.this, error.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
    
                public void onFacebookError(FacebookError error) {
                    Toast.makeText( LoginPage.this, "Something went wrong. Please try again.1"+error.toString(), Toast.LENGTH_LONG).show();
                }
    
                public void onError(DialogError error) {
                    Toast.makeText( LoginPage.this, "Something went wrong. Please try again.2"+error.toString(), Toast.LENGTH_LONG).show();
                } 
    
                public void onCancel() {
                    Toast.makeText( LoginPage.this, "Something went wrong. Please try again.3", Toast.LENGTH_LONG).show();
                }
                /******  Facebook Login End  *******/
    
            }
    

提交回复
热议问题