0
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 DB 中查询UserNamePassword+的现有值salt(with md5 hash)

所以我复制了一些代码来创建一个数据库助手类(DBAdapter),它有非常基本的方法来处理数据库。

现在的问题是在MainActivity.java文件中。代码中标记为“此行”的行给出了错误构造函数 DBAdapter(new View.OnClickListener(){}) 未定义。我不知道这是什么意思。我应该怎么做才能让它工作?

4

2 回答 2

2

我只能猜测尝试 MainActivity.this ,有预感..

DBAdapter db = new DBAdapter(MainActivity.this);
于 2013-03-07T23:22:39.047 回答
1

我建议阅读更多有关 android 工作原理的信息。在这种特殊情况下,您需要了解Contexts,但我将从这里开始。

如前所述,您可以使用MainActivity.this为 DBAdapter 提供正确的应用程序上下文。

于 2013-03-07T23:30:36.257 回答