检查MySQL服务器中的详细信息

时间:2016-03-11 14:07:26

标签: java php android mysql

我目前正在使用Android Studio编写我的第一个Android应用。我有一个登录和注册页面。我能够将用户添加到MySQL服务器,但现在我正在努力确保用户不会发生两次。我在我的寄存器活动中实现了一个布尔方法,它在执行寄存器操作之前检查它。但是,我不确定如何通过PHP文件接收响应,因此此方法可以正常工作

这是我的Register活动类中的方法:

private Boolean notUser(User user) {
    boolean exists = true;
    try {
        URL url = new URL("http://point_collector.netau.net/checkUser");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        //Identifies the objects to be passed for the PHP files
        Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("username", user.username)
                .appendQueryParameter("email", user.email);

        String query = builder.build().getEncodedQuery();
        //Records entry
        Log.d("query", query);
        //Opens an output stream to send the data to be verified
        // and then closes the all output streams and flushes the output writer
        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(query);
        writer.flush();
        writer.close();
        os.close();
        //saves the response code to ensure connection was succesful
        int code = conn.getResponseCode();
        Log.d("code", Integer.toString(code));

        //Opens an input stream to retrieve verified data back from the server
        //Starts a String Builder to read the data off the input
        //Closes the BufferedReader once it has finished building the string
        InputStream responseStream = new BufferedInputStream(conn.getInputStream());
        BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = responseStreamReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        responseStreamReader.close();

        String response = stringBuilder.toString();
        Log.d("response",response);

        if(response.contains("This user already exist")) {
            Toast.makeText(Register.this, "Username or email already exist, please try again", Toast.LENGTH_SHORT).show();
            exists = false;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return exists;
}

这是我的PHP文件:

<?php
$con=mysqli_connect("host","username","password","xxxxxx");

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

    $parentUExists = mysqli_query($con, "SELECT username FROM Parent");
    $childUExists = mysqli_query($con, "SELECT username FROM Child");

    $parentEExist = mysqli_query($con, "SELECT email FROM Parent");
    $childEExist = mysqli_query($con, "SELECT email FROM Child");

    if(($email == $parentEExist || $email == $childEExist) && ($username == $parentUExist || $username == $childUExist)) {
       echo "This user already exist";
       mysqli_close($con);
    }
    else {
       echo "Ok";
       mysqli_close($con);
   }
?>

如果有人能给我一些建议,我会非常感激,因为我很困惑。

谢谢

0 个答案:

没有答案