开发者

Android: how to create login page which is authenticate from database

I've 3 java file i)DBAdapter.java ii) signup.java iii) login.java and four one is for menu but not necessary to mention it here.

Now i want to do that when user come to login page and enter username and password in editview, check that data in database and redirect to menu if match found. I've no idea how to do this, here is my code file of DBAdapter.java and login.java

DBAdapter.java

public class DBAdapter 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_FIRSTNAME = "FirstName";
    public static final String KEY_LASTNAME = "LastName";
    public static final String KEY_USERNAME = "UserName";
    public static final String KEY_PASSWORD = "Password";    
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "master";
    private static final String DATABASE_TABLE = "register";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table titles (_id integer primary key autoincrement, "
        + "FirstName text not null, LastName text not null, " 
        + "UserName text not null, Password text not null);";

 开发者_运维问答   private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }

login.java

public class login extends Activity {

    private EditText username;
    private EditText password;

    public String user_name;
    public String pass_word;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        username = (EditText) findViewById(R.id.ev_unameLogin);
        password = (EditText) findViewById(R.id.ev_passwordLogin);

        final Button button = (Button) findViewById(R.id.btn_login);
        button.setOnClickListener(new View.OnClickListener() {           
            public void onClick(View v) {              

                user_name = username.getText().toString();
                pass_word = password.getText().toString();

                Intent goToNextActivity = new Intent(getApplicationContext(), menu.class);
                startActivity(goToNextActivity);


            }
        });                 


    }
}

So what should i've update in both files..

Thanks a lot in advance


In the database class you need to create a method to access the database, eg.

public boolean validateUser(String username, String password){
   Cursor c = getReadableDatabase().rawQuery(
            "SELECT * FROM " + TABLE_NAME + " WHERE "
                    + USER_NAME + "='" + username +"'AND "+PASSWORD+"='"+password+"'" ,  null);
   if (c.getCount>0)
      return true;
      return false;
}

You will of course need to create the database table with something like

public void createTable(SQLiteDatabase database) {
    String creationString = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
            + " ( _id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + USER_NAME + " TEXT NOT NULL, "
            + PASSWORD + " TEXT NOT NULL, "
           ");";
    database.execSQL(creationString);
}

And have variables such as TABLE_NAME which is obviously the name you want to give to the table, USER_NAME which represents the name for the username column, and the same for PASSWORD.

You need to call that createTable() method in the onCreate of the database.

To use the database with your login button you can do:

if (myDatabase.vaidateUser(user_name,pass_word)) {
    Intent goToNextActivity = new Intent(getApplicationContext(), menu.class);
    startActivity(goToNextActivity);

}
else
   Toast.makeText(mContext, "Wrong username/password", Toast.LENGTH_LONG).show();

Hope this helps. I have given you a good starting block. Theres some other stuff you will need to work out yourself, but this should give you a good idea of how it works and where to go next (like adding the records to the database etc). You will need to create a few variables etc, but that isn't difficult!


public void onClick(View v) method should call a method to validate the user.

public void onClick(View v) {

            user_name = username.getText().toString();
            pass_word = password.getText().toString();
            if( validate(user_name, pasass_word));
             {               
              Intent goToNextActivity = new Intent(getApplicationContext(), menu.class);
             startActivity(goToNextActivity);
          }
        }




public boolean validate(String name,String password) {  
    String url = "http://n.n.n.n:nnnn/WebServicePro/services/Authentication";/*give Ip addres:portnumber*/
    String nameSpace="http://com.xxx.yy"; /**give proper  namespace*/
    String methoName= "authenticate";   /*This method is an operation in the webservice*/   
    SoapObject request = new  SoapObject(nameSpace,methoName);  

    request.addProperty("in0",name);
    request.addProperty("in1",password);

    try {
    resultsRequestSOAP = makeSoapCall(request, "", url);
       if(resultsRequestSOAP.toString().equalsIgnoreCase("true")){      
      return true;
            }
    else{     /*handle error*/}
    }catch (Exception exp) {            
        /*handle exception*/}
    return false;
}   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜