I have an app with a facebook login (4.2), when the user logs in, I pass his data (name, birthday, etc) to my MySql online database. My problem is that on some devices I get empty strings, and I can't figure out why?
The user data is sent to the server in the onCompleted() method, inside onSucces() after it is saved to preferences:
public class SplashActivity extends Activity {
private LoginButton loginButton;
CallbackManager callbackManager;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.splash);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends"));
editor = this.getSharedPreferences("UserInfo", Context.MODE_PRIVATE).edit();
isLoggedIn();
if (isLoggedIn()) {
loginButton.setVisibility(View.GONE);
new CountDownTimer(3000, 5000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
finish();
}
}.start();
} else {
}
// Callback registration
loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String id = (String) object.get("id");
String name = (String) object.get("name");
String email = (String) object.get("email");
String birthday = (String) object.get("birthday");
editor.putString("id", id);
editor.putString("name", name);
editor.putString("email", email);
editor.putString("birthday", birthday);
editor.commit();
SendUserInfo sender = new SendUserInfo(getApplication());
sender.sendUserInfo();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name, email, gender, birthday");
request.setParameters(parameters);
request.executeAsync();
finish();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(getApplication(),
"Va rugam sa incercati mai tarziu.",
Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
public boolean isLoggedIn() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null;
}
}
Class to upload the data:
public class SendUserInfo {
Context context;
private int refCode;
public SendUserInfo(Context context) {
this.context = context;
}
public void sendUserInfo () {
SharedPreferences preferences = context.getSharedPreferences("UserInfo",
Context.MODE_PRIVATE);
String regId = preferences.getString("userRegId", null);
String id = preferences.getString("id", "null");
String name = preferences.getString("name", null);
String email = preferences.getString("email", null);
String birthday = preferences.getString("birthday", null);
Random r = new Random();
refCode = r.nextInt(9000 - 6000) + 456;
final int DEFAULT_TIMEOUT = 10000;
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(DEFAULT_TIMEOUT);
RequestParams params = new RequestParams();
params.put("regId", regId);
params.put("fbId", id);
params.put("name", name);
params.put("email", email);
params.put("birthday", birthday);
params.put("refCode", String.valueOf(refCode));
client.post("http://www.edmondvarga.com/laborator/save-user-info.php",
params, new AsyncHttpResponseHandler() {
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
sendUserInfo();
}
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
}
});
}
}
Some users may not have valid emails or birthdays (even if they give you permission), so you always need to handle empty fields. The only guarantee you'll get is a unique ID per user.