在Android设备上显示我的php echo

时间:2016-01-28 15:22:14

标签: java php android mysql android-studio

我想在我的数据库中存在用户名或电子邮件时显示我的php回音

$sql = "SELECT * FROM user WHERE username='$username' OR email='$email'";

    $check = mysqli_fetch_array(mysqli_query($con,$sql));

        if(isset($check)){
            echo "username or email already exist";
        }else {

            $statement = mysqli_prepare($con, "INSERT INTO User (username, email, age, password) VALUES (?, ?, ?, ?)");
            mysqli_stmt_bind_param($statement, "ssis", $username, $email, $age, $password);
        }

mysqli_stmt_execute($statement);

mysqli_stmt_close($statement);
mysqli_close($con);

当电子邮件或用户名已存在但未显示我的回应并退出我的注册活动时,它不会创建帐户。

这是我的ServerRequests.java

 @Override
    protected Void doInBackground(Void... params) {
        ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        dataToSend.add(new BasicNameValuePair("username", user.username));
        dataToSend.add(new BasicNameValuePair("email", user.email));
        dataToSend.add(new BasicNameValuePair("password", user.password));
        dataToSend.add(new BasicNameValuePair("age", user.age + ""));

        HttpParams httpRequestParams = getHttpRequestParams();

        HttpClient client = new DefaultHttpClient(httpRequestParams);
        HttpPost post = new HttpPost(SERVER_ADDRESS
                + "Register.php");


        try {
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            client.execute(post);

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

        return null;

    }

    private HttpParams getHttpRequestParams() {
        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);
        return httpRequestParams;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        progressDialog.dismiss();
        userCallBack.done(null);
    }

1 个答案:

答案 0 :(得分:0)

PHP上用于打印的

echo方法 如果你想发送消息到你的应用程序 只需使用变量$ msg而不是echo 并在您的应用中请求$ msg

$sql = "SELECT * FROM user WHERE username='$username' OR email='$email'";

    $check = mysqli_fetch_array(mysqli_query($con,$sql));

        if(isset($check)){
            $msg = "username or email already exist";
        }else {

            $statement = mysqli_prepare($con, "INSERT INTO User (username, email, age, password) VALUES (?, ?, ?, ?)");
            mysqli_stmt_bind_param($statement, "ssis", $username, $email, $age, $password);
            $msg = "Created";
        }
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);