java - 如何防止注册 Activity 中使用 "Username already taken"重复输入

标签 java android

我想包含一个代码,这样当用户注册一个已在我的应用程序中使用的用户名时,他/她将收到一条消息,提示“用户名已被占用”。

Register.java

public class Register extends AppCompatActivity {
EditText regEmail, regPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    regEmail = (EditText)findViewById(R.id.reg_email);
    regPassword = (EditText)findViewById(R.id.reg_password);
}
public void OnReg(View view) {
    String strEmail = regEmail.getText().toString();
    String strPassword = regPassword.getText().toString();
    String type = "register";
    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(type, strEmail, strPassword);
}}

BackgroundWorker.java

public class BackgroundWorker extends AsyncTask<String,Void,String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx){
        context = ctx;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://10.93.22.231/login.php";
        String register_url = "http://10.93.22.231/register.php";
        if (type.equals("login")){
            try {
                String email = params[1];
                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String result = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (type.equals("register")){
            try {
                String regEmail = params[1];
                String regPassword = params[2];
                URL url = new URL(register_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(regEmail,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(regPassword,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String result = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        if (result.contains("success")) {
            Intent intent = new Intent(context, MainActivity.class);
            context.startActivity(intent);
        } else {
            alertDialog.show();
        }
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

Register.php

<?php
require "conn.php";

$email = $_POST["email"];
$password = $_POST["password"];

$mysql_qry = "INSERT INTO users (email, password) VALUES ('$email','$password')";

if($conn->query($mysql_qry) === TRUE){
echo "Insert successful";
} else {
echo "Insert failed, please try again.";
}
$conn->close();
?>

最佳答案

Toast.makeText(YourActivtyOrContext,"Username Taken", Toast.LENGTH_LONG).show();

关于java - 如何防止注册 Activity 中使用 "Username already taken"重复输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41582693/

相关文章:

android - 如何在可扩展 ListView 中折叠组 View 上方和下方的所有 subview ?

android - 如何从应用程序循环/重复 Android 中的当前铃声?

android - 警告 : API 'variant.getMappingFile()' is obsolete and has been replaced with 'variant.getMappingFileProvider()'

Android Emulator 在硬件加速下不启动,linux/ubuntu

java - 我如何使用一对多的多次关系?

java - 收到 ConcurrentModificationException 但我没有删除

java - 如何在 Apache Tomee 下配置 ActiveMQ 内存设置?

java - Amazon S3 客户端未列出存储桶中的所有文件夹

java - Spring-Security:使用自定义身份 validator 和自定义登录页面

java - Vaadin 中的 ENTER 快捷方式和 TextArea