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
文件中。代码中标记为“此行”的行显示错误。构造函数DBAdapter(new View.OnClickListener(){})未定义。我不知道这是什么意思。我应该怎么做才能使其正常工作? 最佳答案
我只能猜
试试MainActivity.this,预感。
DBAdapter db = new DBAdapter(MainActivity.this);