2015-08-13 87 views
0

我想要一種方法使用我的用戶名或我的電子郵件登錄到我的Parse帳戶。我現在的代碼只能使用我的用戶名。雖然根據日誌,我的電子郵件被檢測到。無法使用電子郵件登錄到解析帳戶Android

下面是代碼,

private class SignInOnClickListener implements View.OnClickListener { 

    @Override 
    public void onClick(View v) { 
     // Get the username and password from the view 
     final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase(); 
     final String password = mPasswordEtxt.getText().toString(); 
     final String email = mUsernameEmailEtxt.getText().toString().toLowerCase(); 

     if (isFormInputValid(username_email, password)) { 
      if (username_email.indexOf('@') != -1) { // HERE! 
       Log.d("detector", "username_email detected as email:" + email.toString()); 
       ParseUser.logInInBackground(email, password, new LogInCallback() { 
        public void done(ParseUser user, ParseException e) { 
         if (user != null) { 
          // Hooray! The user is logged in. 
          Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
          startActivity(intent); 
          finish(); 
         } 
        } 
       }); 
      } else { 
       Log.d("detector", "username_email detected as username:" + username_email.toString()); 
       ParseUser.logInInBackground(username_email, password, new LogInCallback() { 
        public void done(ParseUser user, ParseException e) { 
         if (user != null) { 
          // Hooray! The user is logged in. 
          Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
          startActivity(intent); 
          finish(); 
         } else { 
          findViewById(R.id.error).setVisibility(View.VISIBLE); 
          Log.d("error", "username or email invalid"); 
         } 

        } 
       }); 
      } 

     } 
    } 
} 

Layout.xml:由@Danielson

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/black" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="false" 
     android:background="@color/black" 
     android:orientation="vertical" 
     android:padding="15dp"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="15dp" 
      android:orientation="vertical"> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="15dp" 
      android:orientation="vertical"> 

      <EditText 
       android:id="@+id/username_email" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="@string/username" 
       android:inputType="text" 
       android:maxLength="30" 

       android:singleLine="true" 
       android:textColor="@color/off_white" 
       android:textColorHint="@color/off_white" /> 

      <EditText 
       android:id="@+id/password" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="@string/password" 
       android:inputType="textPassword" 
       android:maxLength="30" 


       android:singleLine="true" 
       android:textColor="@color/off_white" 
       android:textColorHint="@color/off_white" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <Button 
       android:id="@+id/btn_sign_in" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="5dp" 
       android:layout_weight="50" 
       android:background="@color/black" 
       android:text="@string/sign_in" 
       android:textColor="@color/green" /> 

      <Button 
       android:id="@+id/btn_sign_up" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:layout_marginStart="5dp" 
       android:layout_weight="50" 
       android:background="@color/black" 
       android:text="@string/sign_up" 
       android:textColor="@color/green" /> 
     </LinearLayout> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/error" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/container" 
     android:layout_centerHorizontal="true" 
     android:background="@color/red" 
     android:focusableInTouchMode="false" 
     android:gravity="center" 
     android:padding="8dp" 
     android:textColor="@color/white" 
     android:textStyle="bold" 
     android:visibility="gone" /> 
</RelativeLayout> 
請求

更新:定了!您必須請求與電子郵件關聯的用戶名才能登錄。

下面是最終代碼:

private class SignInOnClickListener implements View.OnClickListener { 

    public void onClick(View v) { 
     // Get the username and password from the view 
     final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase().trim(); 
     final String password = mPasswordEtxt.getText().toString().trim(); 
     if (isFormInputValid(username_email, password)) { 
      if (username_email.indexOf('@') != -1) { 
       ParseQuery<ParseUser> query = ParseUser.getQuery(); 
       query.whereEqualTo("email", username_email); 
       query.getFirstInBackground(new GetCallback<ParseUser>() { 
        public void done(ParseUser object, ParseException e) { 
         if (object == null) { 
          TextView error = (TextView) findViewById(R.id.error); 
          error.setVisibility(View.VISIBLE); 
          error.setText(getString(R.string.error_sign_in)); 

          Log.d("error", "The getFirst request failed. Probably because no associated account found"); 
         } else { 
          String actualUsername = (String) object.get("username"); 
          ParseUser.logInInBackground(actualUsername, password, new LogInCallback() { 
           public void done(ParseUser user, ParseException e) { 
            if (user != null) { 
             // Hooray! The user is logged in. 
             Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
             startActivity(intent); 
             finish(); 
            } else { 
             TextView error = (TextView) findViewById(R.id.error); 
             error.setVisibility(View.VISIBLE); 
             error.setText(getString(R.string.error_sign_in)); 

             Log.d("error", "email or password invalid"); 
            } 
           } 
          }); 
         } 
        } 
       }); 

      } else { 
       Log.d("detector", "username_email detected as username:" + username_email); 
       ParseUser.logInInBackground(username_email, password, new LogInCallback() { 
        public void done(ParseUser user, ParseException e) { 
         if (user != null) { 
          // Hooray! The user is logged in. 
          Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
          startActivity(intent); 
          finish(); 
         } else { 
          TextView error = (TextView) findViewById(R.id.error); 
          error.setVisibility(View.VISIBLE); 
          error.setText(getString(R.string.error_sign_in)); 

          Log.d("error", "username or password invalid"); 
         } 

        } 
       }); 
      } 
     } 
    } 
} 
+0

你可以發佈佈局xml嗎? – Danielson

+0

當然。 @Danielson – Chehanr

+0

我沒有看到任何明顯的錯誤,下列哪些變量具有值? 'username_email','password','email'? – Danielson

回答

0

解析只接受登錄用戶名和密碼是logInInBackground方法需要用戶名和密碼。

+0

修正了它!謝謝你的信息,斯里蘭卡同胞。 @hashan – Chehanr

+0

太棒了。別客氣。很高興我能幫上忙。 –

相關問題