bbt bbt - 5 months ago 21
Android Question

Android studio stop data from going in the database

I created a register page and i created code that if the username or password or both are empty that they would get an error which works. My problem is that even though one or both of the fields are empty if they click the register button something still goes into the database . What I want to make happen is that if the user clicks the register button and the username and/or password fields are empty then the insert into database process stops .

registeractivity :

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bRegister = (Button) findViewById(R.id.bRegister);

bRegister.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();

Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
if (username.equals("")) {
Toast.makeText(getApplicationContext(), "Please fill in username to sign up ",
Toast.LENGTH_SHORT).show();

return;

} else {

if (password.equals("")) {
Toast.makeText(getApplicationContext(), "Please fill in password to sign up ",
Toast.LENGTH_SHORT).show();

return;

} else {


JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register Failed.")
.setNegativeButton("Try again", null)
.create()
.show();
}}}
} catch (JSONException e) {
e.printStackTrace();
}

}
};
RegisterRequest registerRequest = new RegisterRequest( username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}


Register.php:

$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user (username, password) VALUES (?, ?)");

mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

$response = array();
$response["success"] = true;

print_r(json_encode($response));

Answer

Do not do the request to the server if you fields are empty:

@Override
public void onClick(View v) {
    final String username = etUsername.getText().toString();
    final String password = etPassword.getText().toString();

    if (username.isEmpty()) {
        Toast.makeText(getApplicationContext(), "Please fill in username to sign up ",
                Toast.LENGTH_SHORT).show();
        return;
    }

    if (password.isEmpty()) {
        Toast.makeText(getApplicationContext(), "Please fill in password to sign up ",
                Toast.LENGTH_SHORT).show();
        return;
    } 


    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) {
                    Intent intent = new   Intent(RegisterActivity.this, LoginActivity.class);
                    RegisterActivity.this.startActivity(intent);
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                    builder.setMessage("Register Failed.")
                            .setNegativeButton("Try again", null)
                            .create()
                            .show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };
    RegisterRequest registerRequest = new RegisterRequest( username, password, responseListener);
    RequestQueue queue =  Volley.newRequestQueue(RegisterActivity.this);
    queue.add(registerRequest);
}

Also I recommend you to set responseListener as a class variable, because you are creating a new instance each time you click on the button.

Comments