Evening
Currently my system allows the user to enter their username and password, if these are entered correctly, their role will be selected from the database and displayed in a textview.
However I'm also attempting to allow the user to move to the menu page if their login details are correct. My issue is that the user is being redirected to the main page when they click the login button regardless of whether their details are correct. I attempted to surround the intent with an if clause however that had no affect.
public class SigninActivity extends AsyncTask<String,Void,String>{
private TextView roleField;
private Context context;
public SigninActivity(Context context,TextView roleField) {
this.context = context;
this.roleField = roleField;
}
protected void onPreExecute(){
}
@Override
protected String doInBackground(String... arg0) {
try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link = "*";
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}
catch(IOException e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
this.roleField.setText(result);
}
}
With the login calls the sign in constructor in the mainactvity page
public void loginPost(View view){
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
new SigninActivity(this,role).execute(username,password);
if(role!= null){
Intent i = new Intent(this, MainScreen.class);
startActivity(i);
}else{
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}