So,I simply made a login screen where the data comes from an external server. All of that happens in the background method.it all works correctly except for onPost and onPre methods. I don't think they are doing their job.
while the onBackground method is running i would like to:
1.Show a message if the data is right or wrong.
2.Show a custom progress dialog i made while doinbackground is running.
But neither of them work,any ideas?
Here is my code!
public class DoLogin extends AsyncTask<String,String,String>
{
String message = "";
Boolean isSuccess = false;
String userid = dename.getText().toString();
String password = pass.getText().toString();
@Override
protected void onPreExecute() {
progressDialog.showProgress(SignInAr.this,"Loading...",false);
}
@Override
protected String doInBackground(String... params) {
new Thread(new Runnable() {
@Override
public void run() {
if(userid.trim().equals("")|| password.trim().equals(""))
message = "Please enter Username and Password";
else
{
try {
Connection con = connectionClass.CONN();
if (con == null) {
message = "Error in connection with SQL server please contact the administrator";
isSuccess = false;
} else {
String query = "SELECT * FROM CustomerData where CustomerMobile='" + userid + "' and CustomerPassword='" + password + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
{
String id = rs.getString("CustomerID");
String mob = rs.getString("CustomerMobile");
String lng = getIntent().getStringExtra("lng");
String lat = getIntent().getStringExtra("lat");
message = "Welcome " + userid;
Intent intent = new Intent(SignInAr.this,agzakhana_mainAr.class);
intent.putExtra("lat",lat);
intent.putExtra("lng",lng);
intent.putExtra("nameid",id);
String getname = rs.getString("CustomerName");
intent.putExtra("nameofcus",getname);
intent.putExtra("mob",mob);
startActivity(intent);
finish();
isSuccess=true;
}
else
{
message = "Wrong info";
isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
message = "Exceptions"+ex;
}
}
}
}).start();
return message;
}
@Override
protected void onPostExecute(String r) {
progressDialog.hideProgress();
System.out.println("Message is: "+ r + "and isSuccess = "+ isSuccess);
if(!isSuccess) {
Toast.makeText(SignInAr.this,r,Toast.LENGTH_LONG).show();
}
}
}
Here is how i call the asynctask:
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DoLogin doLogin = new DoLogin();
doLogin.execute();
}
});
You return value before thread run. Simple to fix it you shoukd remove Thread
and Runnable
in method doInBackground
because in AsyncTask
it already make doInBackground
run in Background Thread
.