I've been following a youtube tutorial and have come on to a problem, the comments section really didnt help me so here's my problem. The app has a login button when pressed is supposed to start a new activity. My problem is that when entering the correct username and password, the button doesn't do anything but the error message will be displayed if incorrect information is entered. I am unsure what exactly is causing the problem but a lot of the youtube comments have said its the php code, but I cannot find what's wrong with it.
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final TextView tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);
final Button bLogin = (Button) findViewById(R.id.bSignIn);
tvRegisterLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
});
bLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
// Response received from the server
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String name = jsonResponse.getString("name");
int age = jsonResponse.getInt("age");
Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
intent.putExtra("name", name);
intent.putExtra("age", age);
intent.putExtra("username", username);
startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
}
` Now here's the code where the loginActivity uses the loginRequest to connect to the database and compare the filled in fields.
private static final String LOGIN_REQUEST_URL = "http://etc.com/Login2.php";
private Map<String, String> params;
public LoginRequest(String username, String password, Response.Listener<String> listener) {
super(Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
The login button is supposed to bring the user to this page called UserAreaActivity.
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String username = intent.getStringExtra("username");
int age = intent.getIntExtra("age", -1);
TextView tvWelcomeMsg = (TextView) findViewById(R.id.tvWelcomeMsg);
EditText etUsername = (EditText) findViewById(R.id.etUsername);
EditText etAge = (EditText) findViewById(R.id.etAge);
// Display user details
String message = name + " welcome to your user area";
tvWelcomeMsg.setText(message);
etUsername.setText(username);
etAge.setText(age + "");
Here is the php code that the loginRequest will call. `
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM `user` WHERE username= ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $age, $username, $password);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["name"] = $name;
$response["username"] = $username;
$response["age"] = $age;
$response["password"] = $password;
}
echo json_encode($response);
`