成功验证后添加要查看的内容

时间:2015-09-11 16:16:33

标签: java android facebook facebook-sdk-4.0

我有一个应用程序让用户通过Facebook登录。我正在使用Facebook SDK:

public class LoginFragment extends Fragment {

    private TextView mTextDetails;

    private CallbackManager mCallbackManager;
    private static final String TAG = "Facebook";
    private LoginButton login;
    private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.i(TAG, " logged in...");
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile(); //Access the profile who Is the person login in

            if(profile != null) {
                //Start to add things to the View here for the logged in user, but how?
            }
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {
            Log.i(TAG, " Error");
        }

    };

    public LoginFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        mCallbackManager = CallbackManager.Factory.create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.login, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);

        //Ask the use for permission to access friends
        loginButton.setReadPermissions("user_friends");

        //Because we work with fragments
        loginButton.setFragment(this);

        loginButton.registerCallback(mCallbackManager, mCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

用户登录后,我想显示属于登录用户的视图内容。

但是在代码中我可以开始显示登录用户的视图内容吗?

3 个答案:

答案 0 :(得分:0)

在第19行检查是否(profile!= null)后不久。

因为您已经拥有accessToken,所以用户数据已经过验证并被阻止为空。

答案 1 :(得分:0)

参考link,您可以实施 onCompleted(),并且不要忘记保存用户信息。

// When session is changed, this method is called from callback method
private void onSessionStateChange(Session session, SessionState state,
    Exception exception)
{
    final TextView name = (TextView) getView().findViewById(R.id.name);
    final TextView gender = (TextView) getView().findViewById(R.id.gender);
    final TextView location    = (TextView) getView().findViewById(R.id.location);

    if (state.isOpened()) {
    Log.i(TAG, "Logged in...");
    // make request to the /me API to get Graph user
    Request.newMeRequest(session, new Request.GraphUserCallback() {

        // callback after Graph API response with user
        // object
        @Override
        public void onCompleted(GraphUser user, Response response) {
            if (user != null) {
                // Set view visibility to true
                otherView.setVisibility(View.VISIBLE);
                // Set User name 
                name.setText("Hello " + user.getName());
                // Set Gender
                gender.setText("Your Gender: "
                        + user.getProperty("gender").toString());
                location.setText("Your Current Location: "
                        + user.getLocation().getProperty("name")
                                .toString());
            }
        }
    }).executeAsync();
} else if (state.isClosed()) {
    Log.i(TAG, "Logged out...");
    otherView.setVisibility(View.GONE);
}

}

答案 2 :(得分:0)

好像你已经差不多完成了,你需要做的就是在用户成功登录onSuccess回调之后编写另一个获取用户信息的请求,以获取用户名和电子邮件

私人FacebookCallback mCallback =新的FacebookCallback(){

    @Override 
    public void onSuccess(LoginResult loginResult) {
          // get user name and email 
           this.getUserInfo();
        } 
    } 
   ...
}


private void getUserInfo()
{
    final GraphRequest.GraphJSONObjectCallback callback = new GraphRequest.GraphJSONObjectCallback()
    {
        @Override
        public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse)
        {
            if (jsonObject == null)
            {
                return;
            }

            String name = jsonObject.optString("name");
            String email = jsonObject.optString("email");

            // Do something with these data here
        }
    };

    GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), callback);
    Bundle params = graphRequest.getParameters();
    params.putString("fields", "name,email");
    graphRequest.setParameters(params);
    graphRequest.executeAsync();

}

请记住根据您想要的内容添加此代码

//默认权限是public_profile

loginButton.setReadPermissions( “电子邮件”);

您可以在此处了解如何使用Graph API https://developers.facebook.com/docs/graph-api