我想检查我的Android应用程序,如果用户的电子邮件Facebook存在与否,如果它不存在,请将其设置为"找不到邮件"。 代码是正确的但当我点击Facebook按钮时没有发生任何事情,因为我不存在的电子邮件导致错误。 我已经检查了我的用户令牌,我的Facebook帐户没有收到电子邮件。显然,如果没有电子邮件字符串,它就能完美运行。
这是我的代码:
loginButtonFb.setReadPermissions("user_friends", "email");
loginButtonFb.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
new GraphRequest(
AccessToken.getCurrentAccessToken(),
loginResult.getAccessToken().getUserId(),
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONObject data = response.getJSONObject();
login_name = data.getString("name");
String idFb = data.getString("id");
String mailFb = data.getString("email");
String method = "loginFb";
BackgroundTaskLogin backgroundTask = new BackgroundTaskLogin(mContext);
backgroundTask.execute(method, login_name, idFb, mailFb);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
).executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(mContext, "Login attempt canceled.", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(mContext, "Login attempt failed.", Toast.LENGTH_LONG).show();
}
});
答案 0 :(得分:0)
我已经解决了这个问题。我已将String mailFb声明为通用String。 代码的另一部分如下:
loginButtonFb.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
new GraphRequest(
AccessToken.getCurrentAccessToken(),
loginResult.getAccessToken().getUserId(),
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONObject data = response.getJSONObject();
login_name = data.getString("name");
String idFb = data.getString("id");
if(!data.isNull("email")){
mailFb = data.getString("email");
}
mailFb = "Mail not found";
String method = "loginFb";
BackgroundTaskLogin backgroundTask = new BackgroundTaskLogin(mContext);
backgroundTask.execute(method, login_name, idFb, mailFb);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
).executeAsync();
}