在数据库中创建新用户

时间:2015-06-27 05:18:25

标签: android parse-platform

我在使用parse.com创建新用户时出现问题。我的代码似乎正确但是出于某种原因,每当我输入新用户时,它都没有注册..有人可以帮我解决这个问题吗?

    final EditText newuser = (EditText) this.findViewById(R.id.newuser);
    final EditText newuserpassword = (EditText)  this.findViewById(R.id.newuserpassword);
    final EditText EmailAddress = (EditText) this.findViewById(R.id.user_email);



    TextView neighbourView = new TextView(this);

    Button button_test2;
    button_test2 = (Button) this.findViewById(R.id.button);
    button_test2.setOnClickListener(new View.OnClickListener()

    {


        public void onClick(View s) {

            final String newusersname = newuser.getText().toString();
            final String newpasswoord = newuserpassword.getText().toString();
            final String newusermail = EmailAddress.getText().toString();


            ParseUser user = new ParseUser();
            user.setUsername(String.valueOf(newusersname));
            user.setPassword(String.valueOf(newpasswoord));
            user.setPassword(String.valueOf(newusermail));



            user.signUpInBackground(new SignUpCallback() {
                @Override
                public void done( com.parse.ParseException e) {
                    if (e == null) {
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                        startActivity(browserIntent);
                    } else {
                        // Sign up didn't succeed. Look at the ParseException
                        // to figure out what went wrong
                        Toast.makeText(getApplicationContext(), "choose a differnt username or password", Toast.LENGTH_LONG).show();

                    }
                    //if e != null, something went wrong
                }
            });
        }
    });

我的XML是

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="New User Name"
        />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:inputType="textPersonName"
        android:id="@+id/newuser" />

    <!--  Password Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="@string/password"/>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:password="true"
        android:inputType="textPassword"
        android:id="@+id/newuserpassword"
        android:layout_gravity="center_horizontal" />


    <!--  Email Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="E-Mail"/>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:inputType="textEmailAddress"
        android:id="@+id/user_email" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Done"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />

这是我的ParseApplication方法

   public class ParseApplication extends Application {
@Override
public void onCreate() {
    super.onCreate();
    Parse.enableLocalDatastore(this);
    Parse.initialize(this, "xxxxxx ", "xxxx");
    //...Rest of the Parse initializations.
}

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

尝试保存用户的saveInBackground()方法。

       ParseUser user = new ParseUser();
        user.setUsername(String.valueOf(newusersname));
        user.setPassword(String.valueOf(newpasswoord));
        user.setPassword(String.valueOf(newusermail));

        user.saveInBackground();

答案 1 :(得分:0)

我是新手,但我认为问题可能是您尝试分配2个密码,

因为我理解它不能复制的字段并且授予了电子邮件,所以在用户对象中已经构建了一个单独的字段

        user.setUsername(String.valueOf(newusersname));
        user.setPassword(String.valueOf(newpasswoord));
        user.setEmail(String.valueOf(newusermail));

答案 2 :(得分:0)

您可能没有从之前的会话中退出。添加注销逻辑并检查。

替换此

 final String newusersname = newuser.getText().toString();
            final String newpasswoord = newuserpassword.getText().toString();
            final String newusermail = EmailAddress.getText().toString();


            ParseUser user = new ParseUser();
            user.setUsername(String.valueOf(newusersname));
            user.setPassword(String.valueOf(newpasswoord));
            user.setPassword(String.valueOf(newusermail));

用这个

 final String newusersname = newuser.getText().toString();
            final String newpasswoord = newuserpassword.getText().toString();
            final String newusermail = EmailAddress.getText().toString();

if(ParseUser.getCurrentUser()!=null)
        {
            ParseUser.getCurrentUser().logOut();
        }

            ParseUser user = new ParseUser();
            user.setUsername(String.valueOf(newusersname));
            user.setPassword(String.valueOf(newpasswoord));
            user.setPassword(String.valueOf(newusermail));