2012-10-18 41 views
0

我開發了一個登錄表單。使用mysql的Android登錄表單

這是我的web服務代碼:

public class Login { 
    public String authentication(String userName,String password){ 

    String retrievedUserName = ""; 
    String retrievedPassword = ""; 
    String status = ""; 
    try{ 

     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/androidlogin","root","chathura"); 
     PreparedStatement statement = con.prepareStatement("SELECT * FROM user WHERE username = '"+userName+"'"); 
     ResultSet result = statement.executeQuery(); 

     while(result.next()){ 
      retrievedUserName = result.getString("username"); 
      retrievedPassword = result.getString("password"); 
     } 

     if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)){ 
      status = "Success!"; 
     } 

     else{ 
      status = "Login fail!!!"; 
     } 

    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
    return status; 
    } 

} 

這是我的Android代碼:

try{ 
    androidHttpTransport.call(SOAP_ACTION, envelope); 
    SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 
    String status = response.toString(); 
    TextView result = (TextView) findViewById(R.id.tv_status); 
    result.setText(response.toString()); 

    if(status.equals("Success!")){ 

     // ADD to save and read next time 
     String strUserName = userName.getText().toString().trim(); 
     String strPassword = userPassword.getText().toString().trim(); 

     if (null == strUserName || strUserName.length() == 0){ 

      // showToast("Enter Your Name"); 
      userName.setError("username is required!"); 
      isUserValidated = false; 
     } 
     if (null == strPassword || strPassword.length() == 0){ 

      // showToast("Enter Your Password"); 
      isPasswordValidated = false; 
      userPassword.setError("password is required!"); 
     } 
     if(isUserValidated && isPasswordValidated){ 

      Intent intent = new Intent(AndroidLoginExampleActivity.this,TabBarSimple.class); 
      intent.putExtra("login",userName.getText().toString()); 
      startActivity(intent);   
     } 
    } 

    else{ 
     Intent i = new Intent(getApplicationContext(), AndroidLoginExampleActivity.class); 
     startActivity(i); 
    } 
} 
catch(Exception e){ 
    // 
} 

} 
} 

我目前的O/P是:

  • 如果我進入正確的登錄信息,它會顯示「成功」消息,然後轉到下一個Ac Tivity

  • 如果輸入了錯誤的登錄詳細信息,則顯示「登錄失敗」消息。

願望需要的O/P是:

  • 如果我輸入正確的登錄信息,就應該直接進入到下一個活動,應該不會顯示任何成功的消息。

  • 如果輸入錯誤的登錄詳細信息,應顯示「登錄失敗」消息。

請幫我這個。我如何開發這個?

+0

你已經嘗試了什麼!你應該發佈代碼,你已經嘗試過,然後問你遇到的問題。 –

+0

刪除顯示成功消息的代碼。而已。它將在登錄成功後直接切換到下一個活動。 –

+0

什麼是你的** AndroidLoginExampleActivity **和** tabBarActivity ** –

回答

0

正如您所提到的,您不希望在應用程序中使用toast或alert對話框來顯示登錄失敗的消息,因此您可以使用Textview顯示消息,爲此,應該提及textView和Password EditText字段,用任何名稱說,PasswordMessage

現在,嘗試使用下面的代碼,

TextView PasswordMessage = (TextView)findViewById(R.id.PasswordMessage); 
if(isUserValidated && isPasswordValidated) 
{ 
    Intent intent = new Intent(AndroidLoginExampleActivity.this,TabBarSimple.class); 
    intent.putExtra("login",userName.getText().toString()); 
    startActivity(intent); 
} 
else 
{ 
    PasswordMessage.setText("Login Failed, Please enter valid Username or Password"); 
}