简体   繁体   中英

Android : Get and store Facebook name with GraphRequest newMeRequest

I want to get the name of the person connected by facebook in my app. I use this method in on Success :

    private LoginButton loginbutton2;
    private ProfilePictureView fAvatar2;
    private TextView fName2;

private FacebookCallback<LoginResult> mCallback2 = new FacebookCallback<LoginResult>() {
      public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            final Profile profile = Profile.getCurrentProfile();

            GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                    /*fName2.setText(profile.getName());
                    fAvatar2.setProfileId(profile.getId());*/
                }
            }).executeAsync();
            }

}

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_info_account);

        mCallbackManager2 = CallbackManager.Factory.create();
        loginbutton2 = (LoginButton) findViewById(R.id.login_button2);
        loginbutton2.setBackgroundResource(R.drawable.facebookconnexion);
        loginbutton2.setReadPermissions("user_friends");
        loginbutton2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        loginbutton2.registerCallback(mCallbackManager2, mCallback2);


        fName2 = (TextView) findViewById(R.id.facebookName2);
        fAvatar2 = (ProfilePictureView) findViewById(R.id.facebook_avatar2);
}

How can I put the name in my textview (in commentaries)? When I don't comment this line : /*fName2.setText(profile.getName()); I have an error ENOENT. In fact how can I with newMeRequest store the name so that I put it in the textview after?

Thx,

To get the name, do this

In this place

 GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                try {
 fName2.setText(jsonObject.getString("first_name"));
                fAvatar2.setProfileId(jsonObject.getString("profile_id"));<--I am not sure profile_id is the name there do this to see the JSON response
 Log.e("FBGraphResponse", jsonObject.toString());
 } catch(Exception e){e.printStackTrace();}
            }
        }).executeAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM