public class MainActivity extends Activity {
private String md5(String in) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
digest.reset();
digest.update(in.getBytes());
byte[] a = digest.digest();
int len = a.length;
StringBuilder sb = new StringBuilder(len << 1);
for (int i = 0; i < len; i++) {
sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
sb.append(Character.forDigit(a[i] & 0x0f, 16));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText text = (EditText) findViewById(R.id.editText1);
EditText text1 = (EditText) findViewById(R.id.editText2);
String userid = text.getText().toString();
String pass = text1.getText().toString();
Toast.makeText(
MainActivity.this,
"Entered " + userid + " and password entered is "
+ pass, Toast.LENGTH_LONG).show();
pass = md5(pass + "@string/salt");
Toast.makeText(
MainActivity.this,
"Password after adding a salt and md5 hashing is now equal to "
+ pass, Toast.LENGTH_LONG).show();
/* This Line */DBAdapter db = new DBAdapter(this); // This Line.
db.open();
Cursor c = db.getPasswordForUserName(userid);
if (c.moveToFirst()) {
if (c.getString(1) == pass) {
Toast.makeText(MainActivity.this,
"Authentication Succeded", Toast.LENGTH_SHORT)
.show();
// proceed
} else {
Toast.makeText(MainActivity.this, "@string/AuthFail",
Toast.LENGTH_SHORT).show();
// AuthFailure
}
} else {
Toast.makeText(MainActivity.this, "@string/UserNotFound",
Toast.LENGTH_SHORT).show();
// where to from here
}
}
});
}
}
我是Android编程的新手。所以我从很多地方获取了代码片段,然后将它们组合在一起。
基本上我想要做的就是在本地SQLite数据库中查询现有值UserName
和Password
+ salt(with md5 hash)
。
所以我复制了一些代码来创建一个数据库助手类(DBAdapter
),它有很基本的方法来处理数据库。
现在的问题是在MainActivity.java
文件中。代码中标有“this line”的行给出了错误构造函数DBAdapter(new View.OnClickListener(){})未定义。我不知道这意味着什么。我该怎么做才能让它发挥作用?