Android登录验证 [英] Android Login Validation

查看:45
本文介绍了Android登录验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一切,因此在此阶段对我的Android登录感到沮丧,有人可以帮忙吗!我有一个登录屏幕,要求用户输入电子邮件和密码,当他们单击登录时,它需要去检查我的 sqlite 数据库,如果正确,他们将成功登录,如果不正确,他们将被要求输入再试一次!我已经在下面发布了我的代码

I have tried everything and so frustrated at this stage with my android login, can someone help PLEASE! I have a login screen which asks user to enter email and password,when they click login it needs to go and check my sqlite db, if correct they will have successful login if not correct they will be asked to try again! I have posted my code below

登录活动

   package com.example.finalproject;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity{

    EditText EmailAdd;
    EditText Password;
    Button Login;
    Button NewUser;
    private SQLiteAdapter db;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        //addListenerOnButton();

        //Button mNewUser = (Button)findViewById(R.id.btnLogMain);
        //mNewUser.setOnClickListener(this);

        EmailAdd = (EditText)findViewById(R.id.password);
        Password = (EditText)findViewById(R.id.password);

        Login = (Button)findViewById(R.id.btnLogMain);
        Login.setOnClickListener(buttonLoginOnClickListener);
        NewUser = (Button)findViewById(R.id.btnNewUser);
        NewUser.setOnClickListener(buttonNewUserOnClickListener);

    }



    Button.OnClickListener buttonLoginOnClickListener
       = new Button.OnClickListener(){

        @Override
          public void onClick(View arg0) {
            //SQLiteAdapter db = new SQLiteAdapter (LoginActivity.this );
            SQLiteDatabase db = new SQLiteAdapter(LoginActivity.this).openToWrite();
            String email = EmailAdd.getText().toString();
             String password = Password.getText().toString();

            Cursor c = db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email= ? AND password=?", new String[] {email, password});
            if(c.moveToFirst()) {
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                Intent main = new Intent(LoginActivity.this, MainMenuActivity.class);
                startActivity(main);
            } else {
                Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();
            }
            Intent register = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(register);
    }
    };
    /*@Override
    public void onClick(View arg0) {
        Toast.makeText(getApplicationContext(), "Working to here!", Toast.LENGTH_SHORT).show();
         String data1 = EmailAdd.getText().toString();
         String data2 = Password.getText().toString();

        boolean invalid = false;
        //uname == null || uname.length() == 0
        if(data1.equals("")){
            invalid = true;
            Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
        }else if(data2.equals("")){
            invalid = true;
            Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
        }

        if(invalid == false){
            //need to check here for valid email and password

            Intent i_register = new Intent(LoginActivity.this, MainMenuActivity.class);
            startActivity(i_register);
            EmailAdd.setText("");
            Password.setText("");
            finish();
        }
        /*switch(v.getId()){

        case R.id.btnLogMain:
            mEmailAdd = (EditText)findViewById(R.id.email);
            mPassword = (EditText)findViewById(R.id.password);

            String uname = mEmailAdd.getText().toString();
            String pass = mPassword.getText().toString();

            if(uname.equals("") || uname == null){
                Toast.makeText(getApplicationContext(), "email Empty", Toast.LENGTH_SHORT).show();
            }else if(pass.equals("") || pass == null){
                Toast.makeText(getApplicationContext(), "Password Empty", Toast.LENGTH_SHORT).show();
            }else{
                boolean validLogin = validateLogin(uname, pass, LoginActivity.this);
                if(validLogin){
                    System.out.println("In Valid");
                    Intent i = new Intent(LoginActivity.this, MainMenuActivity.class);
                    startActivity(i);
                    finish();
                }
            }
            break;


        }*/
   // }
    //};

*RegisterActivity class*
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
//import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity {

 EditText inputName, inputEmail, inputPassword;
 Button buttonRegister, buttonDeleteAll;
 TextView login;
 private SQLiteAdapter mySQLiteAdapter;
 ListView listContent;

 SimpleCursorAdapter cursorAdapter;
 Cursor cursor;


   /** Called when the activity is first created. */

@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.register_activity);


       inputName = (EditText)findViewById(R.id.name);
       inputEmail = (EditText)findViewById(R.id.email);
       inputPassword = (EditText)findViewById(R.id.password);
       buttonRegister = (Button)findViewById(R.id.register);
       //buttonDeleteAll = (Button)findViewById(R.id.showAll);

       listContent = (ListView)findViewById(R.id.contentlist);

       mySQLiteAdapter = new SQLiteAdapter(this);
       mySQLiteAdapter.openToWrite();

       cursor = mySQLiteAdapter.queueAll();
      // String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_NAME, SQLiteAdapter.KEY_EMAIL,SQLiteAdapter.KEY_PASSWORD};
       /*int[] to = new int[]{R.id.id, R.id.text1, R.id.text2,R.id.text3};
       cursorAdapter =
        new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
       listContent.setAdapter(cursorAdapter);*/

       buttonRegister.setOnClickListener(buttonAddOnClickListener);
       //buttonShowAll.setOnClickListener(buttonShowAllOnClickListener);
       //addListenerOnRegButton();

       TextView loginScreen = (TextView) findViewById(R.id.login);

       // Listening to login 
       loginScreen.setOnClickListener(TextLoginOnClickListener);
}

TextView.OnClickListener TextLoginOnClickListener
= new TextView.OnClickListener(){



@Override
public void onClick(View arg0) {
    Intent i = new Intent(getApplicationContext(), LoginActivity.class);
    startActivity(i);

}
};

   Button.OnClickListener buttonAddOnClickListener
   = new Button.OnClickListener(){



  @Override
  public void onClick(View arg0) {



   String data1 = inputName.getText().toString();
   String data2 = inputEmail.getText().toString();
   String data3 = inputPassword.getText().toString();

   boolean invalid = false;

    if(data1.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Name Missing", Toast.LENGTH_SHORT).show();
    }else if(data2.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
    }else if(data3.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Password Missing", Toast.LENGTH_SHORT).show();
    }

    if(invalid == false){
        mySQLiteAdapter.insert(data1, data2, data3);
        updateList();
           Toast.makeText(RegisterActivity.this, "You are now registered",
                   Toast.LENGTH_SHORT).show();

        Intent i_register = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(i_register);

        inputName.setText("");
        inputEmail.setText("");
        inputPassword.setText("");
        finish();
    }   




  }

   };




  /*Button.OnClickListener buttonShowAllOnClickListener1
   = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
      Intent i = new Intent(RegisterActivity.this, MainMenuActivity.class);
        startActivity(i);
        finish();
  }

   };

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  mySQLiteAdapter.close();
 }*/



 private void updateList(){
  //cursor.requery();
   }

}
    /* public boolean validateLogin(String uemail, String pass, Context context) {

        mydb = new SQLiteAdapter(this);
        SQLiteDatabase db = mydb.openToWrite();
        //SELECT
        String[] columns = {"_id"};

        //WHERE clause
        String selection = "email=? AND password=?";

        //WHERE clause arguments
        String[] selectionArgs = {uemail,pass};

        Cursor cursor = null;
        try{
        //SELECT _id FROM login WHERE email=uemail AND password=pass
        cursor = db.query(SQLiteAdapter.MYDATABASE_TABLE, columns, selection, selectionArgs, null, null, null,null);

        //  startManagingCursor(cursor);

        }catch(Exception e){
            e.printStackTrace();
        }
        int numberOfRows = cursor.getCount();

        if(numberOfRows <= 0){

            Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();
            return false;
        }


        return true;
    }*/



    Button.OnClickListener buttonNewUserOnClickListener
       = new Button.OnClickListener(){
        @Override
          public void onClick(View arg0) {

    }
    };



    }

** SQLiteAdapter **

**SQLiteAdapter **

package com.example.finalproject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteAdapter {

 public static final String MYDATABASE_NAME = "MY_PROJECT_DATABASE";
 public static final String MYDATABASE_TABLE = "MY_USERS_TABLE";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "_id";
 public static final String KEY_NAME = "name";
 public static final String KEY_EMAIL = "email";
 public static final String KEY_PASSWORD = "password";

 //create table MY_DATABASE (ID integer primary key, Content text not null);
 private static final String SCRIPT_CREATE_DATABASE =
  "create table " + MYDATABASE_TABLE + " ("
  + KEY_ID + " integer primary key autoincrement, "
  + KEY_NAME + " text not null, "
  + KEY_EMAIL + " text not null, "
  + KEY_PASSWORD + " text not null);";

 private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;

 private Context context;

 public SQLiteAdapter(Context c){
  context = c;
 }

 public SQLiteAdapter openToRead() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  return this; 
 }

 public SQLiteDatabase openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return sqLiteDatabase;
    }
 /*public SQLiteAdapter openToWrite() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  return this; 
 }*/

 public void close(){
  sqLiteHelper.close();
 }

 public long insert(String name, String email, String password){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_NAME, name);
  contentValues.put(KEY_EMAIL, email);
  contentValues.put(KEY_PASSWORD, password);
  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
 }

 public int deleteAll(){
  return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
 }

 public Cursor queueAll(){
  String[] columns = new String[]{KEY_ID, KEY_NAME, KEY_EMAIL,KEY_PASSWORD};
  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
    null, null, null, null, null);

  return cursor;
 }

 public class SQLiteHelper extends SQLiteOpenHelper {

  public SQLiteHelper(Context context, String name,
    CursorFactory factory, int version) {
   super(context, name, factory, version);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   db.execSQL(SCRIPT_CREATE_DATABASE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub
  }
 } 
}

我已经尝试了很多事情,但是当我按下登录按钮时,出现一条消息不幸停止工作",谢谢您的帮助,将不胜感激

I have tried many things but the when i hit login button a message appears "Unfortunately stopped working" thank u in advance for any help, it will be greatly appreciated

LogCat

The application may be doing too much work on its main thread.
    02-04 00:49:58.305: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.305: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.415: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.415: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.874: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.884: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.894: I/Choreographer(7726): Skipped 73 frames!  The application may be doing too much work on its main thread.
    02-04 00:49:58.894: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.894: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.945: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.975: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.975: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:59.024: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:59.104: D/AndroidRuntime(7726): Shutting down VM
    02-04 00:49:59.104: W/dalvikvm(7726): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
    02-04 00:49:59.154: E/AndroidRuntime(7726): FATAL EXCEPTION: main
    02-04 00:49:59.154: E/AndroidRuntime(7726): java.lang.NullPointerException
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at com.example.finalproject.LoginActivity$2.onClick(LoginActivity.java:165)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.view.View.performClick(View.java:4202)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.view.View$PerformClick.run(View.java:17340)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.os.Handler.handleCallback(Handler.java:725)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.os.Handler.dispatchMessage(Handler.java:92)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.os.Looper.loop(Looper.java:137)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at java.lang.reflect.Method.invoke(Method.java:511)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at dalvik.system.NativeStart.main(Native Method)
    02-04 00:50:07.004: I/Process(7726): Sending signal. PID: 7726 SIG: 9

此代码仍无法正常工作,请有人帮助我!

This code is still not working, Please someone help me!

推荐答案

使用此查询

SELECT email FROM MY_USERS_TABLE WHERE email=? AND password=?

您使用的是?"用 ?仅

you are using '?' use ? only

这篇关于Android登录验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆