您好我正在使用facebook登录。
LandingFragment.java
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(ConstantMethods.isNetworkAvailable()){
GetOrCreateFacebookActiveSession(savedInstanceState);
}
GetKeyHash();
}
//按钮单击侦听器
faceBookLoginButton.setOnClickListener(new onFacebookLoginListener());
// onFacebookLoginListener()
// facebook登录 私有类onFacebookLoginListener实现OnClickListener {
@Override
public void onClick(View v) {
if (ConstantMethods.isNetworkAvailable()) {
hideKeyBoard();
FacebookLoginUtility utility = new FacebookLoginUtility(getActivity());
Session session = Session.getActiveSession();
if (!session.isOpened()) {
new FacebookLoginUtility(getActivity(),LandingFragment.this).loginToFacebook(getActivity());
} else {
utility.fetch_User_Data(getActivity());
}
} else {
ConstantMethods.showErrorDialog(getActivity(), getResources()
.getString(R.string.network_conn_loss));
}
}
}
FacebookLoginUtility.java //只是一个类而非活动
在这里,我复制了我在本课程中实现的所有内容。
public class FacebookLoginUtility {
private Context mContext;
private Session.StatusCallback statusCallback;
public static GraphUser FBuser;
private LandingFragment mLandingFragment;
public FacebookLoginUtility(Context mContext) {
super();
this.mContext = mContext;
}
public FacebookLoginUtility(Context mContext,
LandingFragment landingFragment) {
super();
this.mContext = mContext;
this.mLandingFragment = landingFragment;
}
public void loginToFacebook(Context context) {
if (isNetworkAvailable()) {
Session session = Session.getActiveSession();
// if session is in exceptional state
if (session.getState() == SessionState.CLOSED_LOGIN_FAILED
|| session.getState() == SessionState.OPENING) {
session.closeAndClearTokenInformation();
}
if (!session.isOpened() && !session.isClosed()) {
String[] PERMISSION_ARRAY_READ = { "email", "user_birthday" };
List<String> PERMISSION_LIST = Arrays
.asList(PERMISSION_ARRAY_READ);
session.openForRead(new Session.OpenRequest((Activity) context)
.setPermissions(PERMISSION_LIST).setCallback(
statusCallback));
} else {
Session.openActiveSession((Activity) context, true,
statusCallback);
}
} else {
// showAlertDialog("Please check your internet connection!");
}
}
public void fetch_User_Data(final Context mContext) {
// request a new session with read permissions if session is opened with
// publish permissions
Session session = Session.getActiveSession();
List<String> permissions = session.getPermissions();
try {
if (!isSubsetOf(Arrays.asList("email", "user_birthday"),
permissions)) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
(Activity) mContext, Arrays.asList("email",
"user_birthday"));
session.requestNewReadPermissions(newPermissionsRequest);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Upgraded
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
System.out.println("" + user);
if (user != null) {
FBuser = user;
mLandingFragment.getFacebookUserDetails(user
.getInnerJSONObject().toString());
/*
* setRegisterValuesUsingFacebook(user.getInnerJSONObject()
* .toString());
*/
} else {
Toast.makeText(mContext, "Unable to get user profile data",
Toast.LENGTH_SHORT).show();
}
}
}).executeAsync();
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
// return activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
return (activeNetworkInfo != null)
&& activeNetworkInfo.isConnectedOrConnecting();
}
public static boolean isSubsetOf(Collection<String> subset,
Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
}
在此类的onCompleted()方法中,用户对象获取null值。所以我无法获得用户的详细信息。
从上面的解释,
我如何获取用户详细信息?