I was writing an Android Login Page and i used a Class that extended AsyncTask.
The problem is that it seems that i can't "control" the flow of the Async Task.
If i insert correct username and password, if i click one time when it connects to the database it says that they are incorrect but if i press a second time it works.
Luckily, it does not happen when i insert wrong user or password.
I used a boolean variable that shows me if password and username are correct.
public class LoginPage extends Activity implements OnClickListener {
public boolean loginCorrect=false;
//other code
}
then i implemented the OnClick(View v) function:
public void onClick(View v)
{//switch related code
case(R.id.btn_submit):{
connectToDB(v);
if(loginCorrect)
{loginCorrect=false // If i have to login another time, it will make all the needed checks
Intent intent = new Intent(this, MainPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
finish();
startActivity(intent);
}
else
{Toast.makeText(getApplicationContext(),"Email or password is incorrect",
Toast.LENGTH_SHORT).show();
}
break;
//other code
In connectToDb(v):
public void connectToDB(View view)
{
ConnectAsync task = new ConnectAsync();
task.execute();
}
And in doInBackground
@Override
protected String doInBackground(String... urls) {
String url= //jdbc string
String user=insUser.getText().toString();
String password=insPw.getText().toString();
Connection connessione=null;
Statement statement=null;
ResultSet resultSet=null;
String sql=//sql query
try{ Class.forName //driver loading
connessione=DriverManager.getConnection(url,user,password);
statement=connessione.createStatement();
resultSet=statement.executeQuery(sql);
if(resultSet.next())
if( //i found a tuple with that username and that password
{
loginCorrect=true;
}
connessione.close();
}
catch (SQLException se){
//exception related code
}
catch (ClassNotFoundException cnfe) {
//exception related code
}
return null;
}