Facebook用户到Parse.com

时间:2015-06-17 02:28:10

标签: android facebook parse-platform facebook-login

如何将Facebook用户的数据传输到Parse.com?我将在下面提供的代码构成一个用户,但是当我登录时它没有用数据更新它,为什么它不起作用?

public class LoginActivity extends Activity {

private EditText usernameView;
private EditText passwordView;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);


    LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            onLoginButtonClicked();
        }
    });



    // Set up the login form.
    usernameView = (EditText) findViewById(R.id.etUsername);
    passwordView = (EditText) findViewById(R.id.etPassword);

    // Set up the submit button click handler
    findViewById(R.id.ibLogin).setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            onNormalLoginButton();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}


private void onNormalLoginButton(){
    // Validate the log in data
    boolean validationError = false;
    StringBuilder validationErrorMessage =
            new StringBuilder(getResources().getString(R.string.error_intro)); //please

    if (isEmpty(usernameView)) {
        validationError = true;
        validationErrorMessage.append(getResources().getString(R.string.error_blank_username));//enter username
    }

    if (isEmpty(passwordView)) {
        if (validationError) {
            validationErrorMessage.append(getResources().getString(R.string.error_join));// and
        }
        validationError = true;
        validationErrorMessage.append(getResources().getString(R.string.error_blank_password));//enter password
    }
    validationErrorMessage.append(getResources().getString(R.string.error_end));// .

    // If there is a validation error, display the error
    if (validationError) {
        Toast.makeText(LoginActivity.this, validationErrorMessage.toString(), Toast.LENGTH_LONG) //LENGHT_LONG means how long the message will stand
                .show();
        return;
    }
    // Set up a progress dialog
    final ProgressDialog dlg = new ProgressDialog(LoginActivity.this);
    dlg.setTitle("Please wait.");
    dlg.setMessage("Logging in.  Please wait.");
    dlg.show();
    // Call the Parse login method
    ParseUser.logInInBackground(usernameView.getText().toString(), passwordView.getText()
            .toString(), new LogInCallback() {


        public void done(ParseUser user, ParseException e) {
            dlg.dismiss();
            if (e != null) {
                // Show the error message
                Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
            } else {
                // Start an intent for the dispatch activity
                ParseUser userguy=ParseUser.getCurrentUser();
                boolean validated=userguy.getBoolean("emailVerified");

                if(validated)

                {

                    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
                    installation.put("user", userguy.getObjectId());
                    installation.put("fullname",userguy.getString("fullname"));
                    installation.saveInBackground();
                    openMainActivity();
                }else{
                    Toast.makeText(LoginActivity.this, "You need to confirm your Email!",Toast.LENGTH_LONG).show();
                }

            }
        }
    });
}


private void onLoginButtonClicked() {
    List<String> permissions = Arrays.asList("email", "user_about_me");
    ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
        @Override
        public void done(ParseUser user, ParseException err) {
            if (user == null) {
                Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
            } else if (user.isNew()) {
                Profile profile = Profile.getCurrentProfile();
                user.put("gender", "");
                user.put("location", "");
                user.put("age", "");
                user.put("meet", "");
                user.put("status", "");
                user.put("link", "");
                user.put("fullname", profile.getName());
                user.setEmail("");
                user.signUpInBackground();
                ParseInstallation installation = ParseInstallation.getCurrentInstallation();
                installation.put("user", user.getObjectId());
                installation.put("fullname", user.getString("fullname"));
                installation.saveInBackground();
                openMainActivity();
            } else {
                Log.d("MyApp", "User logged in through Facebook!");
                ParseInstallation installation = ParseInstallation.getCurrentInstallation();
                installation.put("user", user.getObjectId());
                installation.put("fullname", user.getString("fullname"));
                installation.saveInBackground();
                openMainActivity();

            }
        }
    });

}


private void openMainActivity(){
    Intent intent = new Intent(LoginActivity.this, DispatchActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

private boolean isEmpty(EditText etText) {
    if (etText.getText().toString().trim().length() > 0) {
        return false;
    } else {
        return true;
    }
}


public void signUp(View view) {
    Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
}

忽略onNormalLoginButton()它有效,onloginButtonClicked()就是问题。

编辑: 我试过了这个,但它也没有用。

user.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if(e!=null){
                            Toast.makeText(LoginActivity.this,"User data didn't save!",Toast.LENGTH_SHORT).show();
                        }else{
                        openMainActivity();}
                    }
                });

1 个答案:

答案 0 :(得分:1)

请看下面的代码。为什么要调用user.signUpInBackground();再次? 我认为它应该是 user.saveInBackground(); 另外,请检查字段的数据类型以确保输入正确的数据类型(例如:数字的int / float / double)

&#13;
&#13;
else if (user.isNew()) {
                Profile profile = Profile.getCurrentProfile();
                user.put("gender", "");
                user.put("location", "");
                user.put("age", "");
                user.put("meet", "");
                user.put("status", "");
                user.put("link", "");
                user.put("fullname", profile.getName());
                user.setEmail("");
                user.signUpInBackground();
                ParseInstallation installation = ParseInstallation.getCurrentInstallation();
                installation.put("user", user.getObjectId());
                installation.put("fullname", user.getString("fullname"));
                installation.saveInBackground();
                openMainActivity();
            }
&#13;
&#13;
&#13;

从解析文档中,user.isNew() - &gt;用户注册并通过Facebook登录!

&#13;
&#13;
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
  @Override
  public void done(ParseUser user, ParseException err) {
    if (user == null) {
      Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
    } else if (user.isNew()) {
      Log.d("MyApp", "User signed up and logged in through Facebook!");
    } else {
      Log.d("MyApp", "User logged in through Facebook!");
    }
  }
});
&#13;
&#13;
&#13;

如果它不起作用,请尝试以下代码以了解您的用户是否成功保存

&#13;
&#13;
user.saveInBackground(new SaveCallback() { 
  @Override public void done(ParseException e) { 
    if (e != null) { 
      Log.e("Exception", e.getMessage()); 
      return; 
  } 
  Log.e("OK", "Saved!"); 
}});
&#13;
&#13;
&#13;