首页 > 解决方案 > 登录时出错 仍然提示但仍然登录

问题描述

我正在开发一个具有 2 类用户的移动应用程序。

在我的 php 代码中,我为每个用户分隔了布尔值。success对于客户和success1造型师。

当我按下登录时,错误提示首先是成功菜单配置文件的快速意图。

这是我的代码行LoginRegister.java

private ProgressBar loading;


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


    final EditText userLoginUsername = (EditText) findViewById(R.id.loginUser);
    final EditText userLoginPassword = (EditText) findViewById(R.id.loginPass);
    final Button Login = (Button) findViewById(R.id.buttonLogin);
    final Button Register = (Button) findViewById(R.id.buttonRegister);

    loading = findViewById(R.id.loadinglogin);

    //login
    Login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String username = userLoginUsername.getText().toString();
            final String password = userLoginPassword.getText().toString();

            if(!username.isEmpty() && !password.isEmpty()) {
                Login.setVisibility(View.GONE);
                loading.setVisibility(View.VISIBLE);
                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");
                            boolean success1 = jsonResponse.getBoolean("success1");

                            //Client's Log in
                            if (success) {

                                    //gikan sa php (green ones) to strings sa android
                                    String username = jsonResponse.getString("username");
                                    String name = jsonResponse.getString("name");
                                    String number = jsonResponse.getString("number");
                                    String gender = jsonResponse.getString("gender");
                                    String address = jsonResponse.getString("address");
                                    String occupation = jsonResponse.getString("occupation");
                                    String birth_date = jsonResponse.getString("birth_date");
                                    String user_type = jsonResponse.getString("user_type");

                                    Intent intent = new Intent(LoginRegister.this, ProfileActivity.class);

                                //from strings to pass sa lain intents.
                                intent.putExtra("username",username);
                                intent.putExtra("number",number);
                                intent.putExtra("name", name);
                                intent.putExtra("gender", gender);
                                intent.putExtra("address", address);
                                intent.putExtra("occupation", occupation);
                                intent.putExtra("birthDate", birth_date);
                                    intent.putExtra("userType", user_type);

                                    LoginRegister.this.startActivity(intent);
                                    finish();

                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(LoginRegister.this);
                                builder.setMessage("Login Failed! Please provide valid username and password or connect to internet.")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                                Login.setVisibility(View.VISIBLE);
                                loading.setVisibility(View.GONE);

                            }

                            //Stylist's Log in
                            if(success1) {

                                    String user_type = jsonResponse.getString("user_type");

                                Intent intent = new Intent(LoginRegister.this, ProfileActivity.class);

                                intent.putExtra("userType", user_type);

                                LoginRegister.this.startActivity(intent);
                                finish();
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(LoginRegister.this);
                                builder.setMessage("Login Failed! Please provide valid username and password or connect to internet.")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                                Login.setVisibility(View.VISIBLE);
                                loading.setVisibility(View.GONE);

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(LoginRegister.this);
                queue.add(loginRequest);
            }else if(username.isEmpty() ){
                userLoginUsername.setError("Please insert a username");
            }else if(password.isEmpty()){
                userLoginPassword.setError("Please put your password");
            }

        }
    });

    //register
    Register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent Register = new Intent(LoginRegister.this, RegisterCustomerOrStylist.class);
            LoginRegister.this.startActivity(Register);


        }
    });

}

PS他们有来自不同表的不同数据。我所做的是我有一个 if 条件,如果success(client) 的布尔值为真,它会传递数据,而它的 else 是alertdialog错误登录。success1在它之后是另一个与客户端具有相同逻辑的(造型师)的 if 语句。

标签: android

解决方案


如果简化,您的代码将如下所示。

//Client's Log in
if (success) {
} else {
    AlertDialog.Builder builder = ...
}

//Stylist's Log in
if(success1) {
} else {
    AlertDialog.Builder builder
}

这意味着如果造型师尝试登录,将显示客户的登录阻止警报对话框,反之亦然。

因此,可能需要一个标志来检查是否存在成功。

boolean successAny = success || suucess1;

//Client's Log in
if (success) {
} else {
    if (!successAny) {
        AlertDialog.Builder builder = ...
    }
}

...

注意。一个人既是客户又是造型师案例不适用于此示例。


推荐阅读