获取Facebook的个人资料详细信息

时间:2014-06-20 06:15:08

标签: android facebook

嘿,我正在使用此代码获取用户配置文件按钮点击android但这段代码无效,因为Session.getActiveSession()返回null ..

点击按钮

btnFbLogin1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //Toast.makeText(getApplicationContext(), "Name: ", Toast.LENGTH_LONG).show();

            Session session = Session.getActiveSession();
            if(session==null){                      
                // try to restore from cache

                session = Session.openActiveSessionFromCache(getApplicationContext());
                //make
            }
            if(session !=null)
            {
            //  session.open(, callback);
                makeMeRequest(session);
                Toast.makeText(getApplicationContext(), "ander", Toast.LENGTH_LONG).show();


            }
        }
    });

方式

private void makeMeRequest(final Session session) {
        // Make an API call to get user data and define a 
        // new callback to handle the response.
        Request request = Request.newMeRequest(session, 
                new com.facebook.Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        // TODO Auto-generated method stub
                         if (session == Session.getActiveSession()) {
                                if (user != null) {
                                    // Set the id for the ProfilePictureView
                                    // view that in turn displays the profile picture.
                                   // profilePictureView.setProfileId(user.getId());
                                    // Set the Textview's text to the user's name.
                                    //userNameView.setText(user.getName());
                                    Toast.makeText(getApplicationContext(), "Name: " + user.getName() + "\nEmail: " + user.getId(), Toast.LENGTH_LONG).show();
                                    //Toast.makeText(getApplicationContext(), user.getName(), duration);
                                }
                            }
                            if (response.getError() != null) {
                                // Handle errors, will do so later.
                            }
                    }
                });
        request.executeAsync();
    }
}

登录代码..

public void loginToFacebook() {
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();
                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error

                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors

                    }

                });
    }
}

我不知道为什么会话为空。首先我登录facebook然后点击此按钮获取个人资料请帮助我。

0 个答案:

没有答案