无法存储Android应用程序在服务器上发送的数据

时间:2015-07-30 19:31:30

标签: php android mysql storage

我目前正在制作一款能够通过帐户注册用户的Android应用。但是,没有任何内容存储到我为我的应用程序托管的服务器中。我重新检查了语法错误或我犯的任何错误,但我似乎没有抓到任何错误。这是我的代码:

<?php

  $con=mysqli_connect("sql303.byethost18.com","xxxxxx","xxxxxx","xxxxxx")
  //I replaced username, password, and database with xxxxxx for obvious reasons.

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


$statement = mysqli_prepare($con, "INSERT INTO demo (username, password, email) VALUES (?, ? ,?) ");
mysqli_stmt_bind_param($statement, "sss", $username, $password, $email);
mysqli_stmt_execute($statement);

mysqli_stmt_close($statement);

mysqli_close($con);
?>

我的注册页面的java代码:

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class Register extends Activity implements View.OnClickListener{

EditText regUsername, regPassword, regPassword2, regEmail;
Button regButton;
TextView viewUsername, viewPassword, viewPassword2, viewEmail, viewRegister;

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

    regUsername = (EditText)findViewById(R.id.regUsername);
    regPassword = (EditText)findViewById(R.id.regPassword);
    regPassword2 = (EditText)findViewById(R.id.regPassword2);
    regEmail = (EditText)findViewById(R.id.regEmail);

    regUsername.setTextColor(Color.WHITE);
    regPassword.setTextColor(Color.WHITE);
    regPassword2.setTextColor(Color.WHITE);
    regEmail.setTextColor(Color.WHITE);

    viewUsername = (TextView)findViewById(R.id.viewUsername);
    viewPassword = (TextView)findViewById(R.id.viewPassword);
    viewPassword2 = (TextView)findViewById(R.id.viewPassword2);
    viewEmail = (TextView)findViewById(R.id.viewEmail);
    viewRegister = (TextView)findViewById(R.id.viewRegister);

    viewUsername.setTextColor(Color.WHITE);
    viewPassword.setTextColor(Color.WHITE);
    viewPassword2.setTextColor(Color.WHITE);
    viewEmail.setTextColor(Color.WHITE);
    viewRegister.setTextColor(Color.WHITE);

    regButton = (Button)findViewById(R.id.regButton);

    regButton.setOnClickListener(this);
}

@Override
public void onClick(View v){
    switch(v.getId()){
        case R.id.regButton:
            String username = regUsername.getText().toString();
            String password = regPassword.getText().toString();
            String password2 = regPassword2.getText().toString();
            String email = regEmail.getText().toString();

            if(password.equals(password2) && username.length() > 0){
                if(email.length() > 0){
                    User createUser = new User(username, password, email);
                    registerUser(createUser);
                }else{
                    User createUser = new User(username, password);
                    registerUser(createUser);
                }
            }else if(!(password.equals(password2))){
                TextView registerMessage = (TextView) findViewById(R.id.registerMessage);
                registerMessage.setText("The passwords did not match. Please try again.");
                registerMessage.setTextColor(Color.RED);
            }else if(username.length() == 0){
                TextView registerMessage = (TextView) findViewById(R.id.registerMessage);
                registerMessage.setText("Please enter an username.");
                registerMessage.setTextColor(Color.RED);
            }else if(password.length() == 0 || password2.length() == 0){
                TextView registerMessage = (TextView) findViewById(R.id.registerMessage);
                registerMessage.setText("Please enter password(s).");
                registerMessage.setTextColor(Color.RED);
            }

            break;
    }
}

private void registerUser(User user){
    ServerRequests serverRequests = new ServerRequests(this);
    serverRequests.storeUserDataInBackground(user, new GetUserCallback() {
        @Override
        public void done(User returnedUser) {
            startActivity(new Intent(Register.this, LogIn.class));
            Toast.makeText(Register.this, "Sucessfully registered!", Toast.LENGTH_LONG).show();
        }
    });
}
}

我不确定是否有必要上传ServerRequests类,因为我不认为这些问题属于该类,我不想用代码压倒页面,但如果有的话需要看课,请告诉我!

感谢您的帮助!!

0 个答案:

没有答案