我想使用我的应用从Facebook获取用户数据。我编写了代码,它登录用户但无法获取用户数据。这是代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AccessToken accessToken = AccessToken.getCurrentAccessToken();
callbackManager = CallbackManager.Factory.create();
if (PrefUtils.getCurrentUser(getActivity()) != null) {
//dont ask for login
Intent homeIntent = new Intent(getActivity(), MainActivity.class);
startActivity(homeIntent);
Toast.makeText(getActivity(), "Intent", Toast.LENGTH_SHORT).show();
//getActivity().finish();
}
mCallBack = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.e(TAG, "onSuccess: " );
// App code
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.e(TAG, "onCompleted: "+object );
Log.e("response: ", response + "");
try{
email = object.getString("email");
name = object.getString("name");
Toast.makeText(getActivity(), "onSuccess", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getActivity(), "welcome " + user.name+" "+name, Toast.LENGTH_LONG).show();
Intent intent=new Intent(getActivity(),MainActivity.class);
startActivity(intent);
Toast.makeText(getActivity(), "Intent", Toast.LENGTH_SHORT).show();
//getActivity().finish();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.e(TAG, "onCancel: " );
}
@Override
public void onError(FacebookException e) {
Log.e(TAG, "onError: " );
}
};
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_sign_in, null);
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setCancelable(false);
skip = (Button) rootView.findViewById(R.id.btn_skip);
loginButton = (LoginButton) rootView.findViewById(R.id.fb_login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));
loginButton.setFragment(this);
btnLogin = (TextView) rootView.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginButton.performClick();
loginButton.setPressed(true);
loginButton.invalidate();
loginButton.registerCallback(callbackManager, mCallBack);
loginButton.setPressed(false);
loginButton.invalidate();
Log.e(TAG, "onClick: ");
}
});
skip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(Splash.intent);
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
它记录用户但无法获取数据。在调试时,日志显示为null mCallback。