AsyncTask不会更改其他变量的值

时间:2015-01-25 01:06:50

标签: java android multithreading jdbc android-asynctask

我正在制作一个使用jdbc连接数据库的Android应用程序,jdbc需要AsyncTask,我读了一个关于它的教程但是,我有各种各样的麻烦!

代码:

// login stuff
Button Login;
EditText Username, Password;
String db_username, db_password;

// database variables
Database d;
ResultSet rs;
Statement stmt;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    getActionBar().hide();

    // defining stuff
    Login = (Button) findViewById(R.id.login);
    Username = (EditText) findViewById(R.id.username);
    Password = (EditText) findViewById(R.id.password);
    db_password = "-1";

    // focus on the enter username
    Username.requestFocus();

    Login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // checking for the password and stuff
            Con deploy = new Con();
            deploy.execute();
            // waiting until data is back
            while (db_password.equals("-1")){   
            }
            // checking the entered password
            if (db_password.equals(Password.getText())) {
                Intent i = new Intent(Login.this, Manager.class);
                i.putExtra("name", db_username);
                startActivity(i);
                finish();
            }
        }

    });

}

// connection class
private class Con extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        d = new Database();
        d.connect();

        try {

            // putting the username and pass in the result set
            stmt = d.createStatement();
            rs = stmt
                    .executeQuery("SELECT username,password from users WHERE username='"
                            + Username.getText() + "'");

        } catch (Exception e) {

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        try {
            if (rs.next()) {
                db_username = rs.getString("username");
                db_password = rs.getString("password");
                System.out.println("check point");
                System.out.println(db_password);
                db_password = "0";

            } else {

                Username.setText("");
                Password.setText("");
                db_password = "0";
            }
        } catch (java.sql.SQLException e) {

            e.printStackTrace();
        }
        super.onPostExecute(result);
    }


} 

经过测试后我发现,在线程内部的值会随着数据库的变化而变化,但在主线程上,两个变量db_username db_password永远不会改变! 我把这些变量设为静态但没有用,请帮忙


解决:我使用它代替AcyncTask并解决了所有问题:

  new Thread(new Runnable() {
                public void run() {


            }).start();

1 个答案:

答案 0 :(得分:-1)

private class Con extends AsyncTask<Void, Void, ResultSet > {

@Override
protected ResultSet doInBackground(Void... params) {

    d = new Database();
    d.connect();
    ResultSet  rs;
    try {

        // putting the username and pass in the result set
        Statement stmt = d.createStatement();
        rs = stmt
                .executeQuery("SELECT username,password from users WHERE username='"
                        + Username.getText() + "'");

    } catch (Exception e) {
       return new ResultSet(); // depends on what you want to do on error 
    }

    return rs;
}

@Override
protected void onPostExecute(ResultSet result) {
    super.onPostExecute(result);
    try {
        if (result.next()) {
            db_username = result.getString("username");
            db_password = result.getString("password");
            System.out.println("check point");
            System.out.println(db_password);
           // db_password = "0"; this always makes it zero at the end of the day

        } else {

            Username.setText("");
            Password.setText("");
            db_password = "0";
        }
    } catch (java.sql.SQLException e) {

        e.printStackTrace();
    }

}

修改也会在您的onclick侦听器中更改此if (db_password.equals(Password.getText().toString())) {

使用这个asnyc ayt ..检查结束标签和东西..