用户注册活动不会将用户添加到数据库

时间:2016-08-07 18:37:36

标签: java php android mysql mysqli

嘿,我对JSON,PHP和MYSQLI非常陌生,所以请在这里帮助我,非常感谢任何建议/帮助。

尝试将新用户注册到类项目的本地数据库,卡在一个愚蠢的错误上。

如果你能提供帮助,如果你能告诉我将来如何不再这样做,我真的很感激xD

RegisterActivity Code:

public class RegisterActivity extends AppCompatActivity {

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

    final EditText etUsername = (EditText)findViewById(R.id.etUsername);
    final EditText etName = (EditText)findViewById(R.id.etName);
    final EditText etSurname = (EditText)findViewById(R.id.etSurname);
    final EditText etEmail = (EditText)findViewById(R.id.etEmail);
    final EditText etPassword = (EditText)findViewById(R.id.etPassword);

    final Button bRegister = (Button)findViewById(R.id.bRegister);



    bRegister.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            final String username = etUsername.getText().toString();
            final String name = etName.getText().toString();
            final String surname = etSurname.getText().toString();
            final String email = etEmail.getText().toString();
            final String password = etPassword.getText().toString();

            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");

                        if(success){
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            RegisterActivity.this.startActivity(intent);
                        }else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("Register Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }

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

            RegisterRequest registerRequest = new RegisterRequest(username, name, surname, email, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
            queue.add(registerRequest);

        }
    });
}
}

RegisterRequest代码:

public class RegisterRequest extends StringRequest {

private static final String REGISTER_REQUEST_URL = "http://*my local host*/Register.php";
private Map<String, String> params;

public RegisterRequest(String username, String name, String surname, String email, String password,
                       Response.Listener<String> listener){

    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
    params = new HashMap<>();
    params.put("username", username);
    params.put("name", name);
    params.put("surname", surname);
    params.put("email", email);
    params.put("password", password);

}

@Override
public Map<String, String> getParams() {
    return params;
}
}

Register.php代码:

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

$statement = mysqli_prepare($con, "INSERT INTO user_info(username, name, surname, email, password) VALUES(?,?,?,?,?)");
mysqli_stmt_bind_param($statement, "sssss", $username, $name, $surname, $email, $password);
mysqli_stmt_execute($statment);

 if (mysqli_connect_errno())
{
   echo "Failed no connect to MySQL: " .mysqli_connect_error();
}

$response = array();
$response["success"] = true;  

echo json_encode($response);
?>

Logcat错误:

08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at com.example.brian.testlogin.RegisterActivity$1$1.onResponse(RegisterActivity.java:49)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at com.example.brian.testlogin.RegisterActivity$1$1.onResponse(RegisterActivity.java:45)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at android.os.Looper.loop(Looper.java:135)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5221)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
08-07 14:23:30.980 12566-12566/com.example.brian.testlogin W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

0 个答案:

没有答案