Android:使用GraphRequest newMeRequest获取并存储Facebook名称

时间:2015-10-18 14:52:41

标签: android facebook facebook-graph-api android-studio

我想在我的应用中获取facebook连接的人的姓名。我在成功中使用了这个方法:

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

如何将该名称放在我的textview中(在评论中)?当我不评论此行时:/*fName2.setText(profile.getName());我有一个错误ENOENT。 实际上我怎么能用newMeRequest存储这个名字,以便我把它放在textview之后?

THX,

1 个答案:

答案 0 :(得分:0)

要获取名称,请执行此操作

在这个地方

<script type="text/JavaScript">
    var uri = "http://localhost:8290/api/products";

    $(document).ready(function () {
        $.getJSON(uri)
            .done(function (data) {
                $.each(data, function (key, item) {
                    $('<li>', { text: formatItem(item) }).appendTo($('#products'));
                });
            })
            .fail(function (jqXHR, textStatus, err) {
                $('#product').text('Error: ' + err);
            });
    });