0
我有一個Facebook登錄(4.2)的應用程序,當用戶登錄時,我將他的數據(姓名,生日等)傳遞到我的MySql聯機數據庫。我的問題是,在一些設備上,我得到空字符串,我不明白爲什麼?發送Facebook用戶數據到服務器失敗 - Android
用戶數據在onCompleted()方法發送到服務器,內部onSucces()被保存到偏好後:
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;
}
}
類上傳數據:
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) {
}
});
}
}
某些用戶可能沒有有效的電子郵件或生日(即使他們給予您許可),所以您總是需要處理空白字段。您將獲得的唯一保證是每位用戶的唯一ID。 –
@MingLi請加上這個答案。 –