问题描述
我如何从Facebook的用户Parse.com获得数据?在code,我将在下面提供让用户,但是当我登录它不与数据更新,它为什么不工作?
公共类LoginActivity延伸活动{私人的EditText usernameView;
私人的EditText passwordView;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_login);
LoginButton loginButton =(LoginButton)findViewById(R.id.login_button);
loginButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){ onLoginButtonClicked();
}
}); //设置登录表单。
usernameView =(EditText上)findViewById(R.id.etUsername);
passwordView =(EditText上)findViewById(R.id.etPassword); //设置提交按钮单击处理程序
findViewById(R.id.ibLogin).setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看视图){ onNormalLoginButton();
}
});
}@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据); ParseFacebookUtils.onActivityResult(要求code,结果code,数据);
}
私人无效onNormalLoginButton(){
//验证日志中的数据
布尔validationError = FALSE;
StringBuilder的validationErrorMessage =
新的StringBuilder(getResources()的getString(R.string.error_intro)); //请 如果(的isEmpty(usernameView)){
validationError =真;
validationErrorMessage.append(getResources()的getString(R.string.error_blank_username)); //输入用户名
} 如果(的isEmpty(passwordView)){
如果(validationError){
validationErrorMessage.append(getResources()的getString(R.string.error_join)); //和
}
validationError =真;
validationErrorMessage.append(getResources()的getString(R.string.error_blank_password)); //输入密码
}
validationErrorMessage.append(getResources()的getString(R.string.error_end)); //。 //如果存在验证错误,显示错误
如果(validationError){
Toast.makeText(LoginActivity.this,validationErrorMessage.toString(),Toast.LENGTH_LONG)// LENGHT_LONG意味着消息将如何超长待机
。显示();
返回;
}
//设置进度对话框
最后ProgressDialog DLG =新ProgressDialog(LoginActivity.this);
dlg.setTitle(请稍等。);
dlg.setMessage(登录,请稍候。);
dlg.show();
//调用解析登录方法
ParseUser.logInInBackground(usernameView.getText()。toString()方法,passwordView.getText()
的ToString(),新LogInCallback(){
公共无效完成(ParseUser用户,ParseException的E){
dlg.dismiss();
如果(E!= NULL){
//显示错误信息
Toast.makeText(LoginActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
}其他{
//开始为派遣活动的意图
ParseUser userguy = ParseUser.getCurrentUser();
布尔验证= userguy.getBoolean(emailVerified); 如果(验证) { ParseInstallation安装= ParseInstallation.getCurrentInstallation();
installation.put(用户,userguy.getObjectId());
installation.put(全名,userguy.getString(全名));
installation.saveInBackground();
openMainActivity();
}其他{
Toast.makeText(LoginActivity.this,你需要确认你的邮件!,Toast.LENGTH_LONG).show();
} }
}
});
}
私人无效onLoginButtonClicked(){
清单<串GT;权限= Arrays.asList(电子邮件,user_about_me);
ParseFacebookUtils.logInWithReadPermissionsInBackground(这一点,权限,新LogInCallback(){
@覆盖
公共无效完成(ParseUser用户,ParseException的错误){
如果(用户== NULL){
Log.d(MyApp的,嗯哦用户取消了Facebook登录);
}否则如果(user.isNew()){
简介配置= Profile.getCurrentProfile();
user.put(性别,);
user.put(位置,);
user.put(时代,);
user.put(满足,);
user.put(身份,);
user.put(链接,);
user.put(全名,profile.getName());
user.setEmail();
user.signUpInBackground();
ParseInstallation安装= ParseInstallation.getCurrentInstallation();
installation.put(用户,user.getObjectId());
installation.put(全名,user.getString(全名));
installation.saveInBackground();
openMainActivity();
}其他{
Log.d(MyApp的,用户登录通过Facebook!);
ParseInstallation安装= ParseInstallation.getCurrentInstallation();
installation.put(用户,user.getObjectId());
installation.put(全名,user.getString(全名));
installation.saveInBackground();
openMainActivity(); }
}
});}
私人无效openMainActivity(){
意向意图=新意图(LoginActivity.this,DispatchActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(意向);
}私人布尔的isEmpty(EditText上etText){
如果(。etText.getText()的toString()修剪()长()0){
返回false;
}其他{
返回true;
}
}
公共无效注册(查看视图){
意向意图=新意图(LoginActivity.this,RegisterActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(意向);
}
}
忽略 onNormalLoginButton()
它的工作原理, onloginButtonClicked()
的问题。
编辑:
我试过,但它也没有工作。
user.saveInBackground(新SaveCallback(){
@覆盖
公共无效完成(ParseException的E){
如果(E!= NULL){
Toast.makeText(LoginActivity.this!用户数据不救,Toast.LENGTH_SHORT).show();
}其他{
openMainActivity();}
}
});
看看下面code。你为什么叫user.signUpInBackground();再次?
我想应该是的 user.saveInBackground();
同时,请检查您的字段的数据类型,以确保你把正确的数据类型(例如:INT /浮点/双精度数为)
否则如果(user.isNew()){\r
简介配置= Profile.getCurrentProfile();\r
user.put(性别,);\r
user.put(位置,);\r
user.put(时代,);\r
user.put(满足,);\r
user.put(身份,);\r
user.put(链接,);\r
user.put(全名,profile.getName());\r
user.setEmail();\r
user.signUpInBackground();\r
ParseInstallation安装= ParseInstallation.getCurrentInstallation();\r
installation.put(用户,user.getObjectId());\r
installation.put(全名,user.getString(全名));\r
installation.saveInBackground();\r
openMainActivity();\r
}
\r
从解析文档,user.isNew() - >用户注册并通过Facebook登录!
ParseFacebookUtils.logInWithReadPermissionsInBackground(这一点,权限,新LogInCallback(){\r
@覆盖\r
公共无效完成(ParseUser用户,ParseException的错误){\r
如果(用户== NULL){\r
Log.d(MyApp的,嗯哦用户取消了Facebook登录);\r
}否则如果(user.isNew()){\r
Log.d(MyApp的,用户注册并通过Facebook登录!);\r
}其他{\r
Log.d(MyApp的,用户登录通过Facebook!);\r
}\r
}\r
});
\r
如果它不能正常工作,请尝试以下code知道,如果你的用户成功或不保存
user.saveInBackground(新SaveCallback(){\r
@覆盖公共无效完成(ParseException的E){\r
如果(E!= NULL){\r
Log.e(异常,e.getMessage());\r
返回;\r
}\r
Log.e(OK,得救了!);\r
}});
\r
How do I get the data from The Facebook user to Parse.com? The code that I will provide below makes a user but it doesn't update it with data when I login, why is it not working?
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);
}
}
Ignore the onNormalLoginButton()
it works, onloginButtonClicked()
is the problem.
Edit:I've tried this but it also didn't work.
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();}
}
});
Look at below code. Why do you call user.signUpInBackground(); again?I think it should be user.saveInBackground();Also, please check the data type of your fields to make sure you put correct data type (Example: int/float/double for Number)
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();
}
From parse document, user.isNew() -> User signed up and logged in through Facebook!
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!");
}
}
});
If it doesn't work, try below code to know if your user is saved successfully or not
user.saveInBackground(new SaveCallback() {
@Override public void done(ParseException e) {
if (e != null) {
Log.e("Exception", e.getMessage());
return;
}
Log.e("OK", "Saved!");
}});
这篇关于Facebook的用户Parse.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!