我正在尝试按照本教程系列进行登录和注册应用程序:https://www.youtube.com/watch?v=JQXfIidfFMo
我遇到的唯一问题是Username字段被放入数据库中为“0”,我浏览了所有内容,我无法理解它!所有其他领域都运作得很好!
activity_register.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mcrlogs.pp.mcrproximitypatrol.RegisterActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/etName"
android:hint="@string/real_name"
android:inputType="textPersonName"
android:layout_below="@+id/imageView"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/etEmail"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:layout_below="@+id/etName"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/etUser"
android:hint="@string/user_name"
android:layout_below="@+id/etEmail"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<Button
android:text="@string/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bRegister"
android:layout_below="@+id/etPassword"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="150dp"
app:srcCompat="@mipmap/security"
android:id="@+id/imageView"
tools:contentDescription="MCR Security Logo"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:contentDescription="@string/logo" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/etPassword"
android:hint="@string/password"
android:inputType="textPassword"
android:layout_below="@+id/etUser"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
RegisterActivity.java:
package com.mcrlogs.pp.test;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etName = (EditText) findViewById(R.id.etName);
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
final EditText etUser = (EditText) findViewById(R.id.etUser);
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 name = etName.getText().toString();
final String email = etEmail.getText().toString();
final String user = etUser.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("Registration Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, email, user, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
RegisterRequest.java:
package com.mcrlogs.pp.test;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
/**
* Created by sbuckley on 12/01/2017.
*/
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "https://www.example.com/Register.php";
private Map<String, String> params;
public RegisterRequest(String name, String email, String user, String password, Response.Listener<String> listener){
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("name", name);
params.put("email", email);
params.put("user", user);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
最后是服务器上的 Register.php 脚本(我认为这是无关紧要的,因为null来自android app端)。
<?php
$con = mysqli_connect("localhost", "root", "password.", "database");
/* check connection */
if (!$con) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$name = $_POST["name"];
$email = $_POST["email"];
$user = $_POST["user"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO users (name, user, email, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $user, $email, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
logcat的:
01-13 07:59:59.563 2598-2598/? I/art: Not late-enabling -Xcheck:jni (already on)
01-13 07:59:59.568 2598-2598/? W/art: Unexpected CPU variant for X86 using defaults: x86
01-13 07:59:59.606 2598-2598/com.mcrlogs.pp.test W/System: ClassLoader referenced unknown path: /data/app/com.mcrlogs.pp.test-1/lib/x86
01-13 07:59:59.614 2598-2598/com.mcrlogs.pp.test I/InstantRun: Instant Run Runtime started. Android package is com.mcrlogs.pp.test, real application class is null.
[ 01-13 07:59:59.631 1541: 1568 D/ ]
HostConnection::get() New Host Connection established 0x96f7f380, tid 1568
01-13 08:00:07.316 2598-2598/com.mcrlogs.pp.test W/System: ClassLoader referenced unknown path: /data/app/com.mcrlogs.pp.test-1/lib/x86
01-13 08:00:08.156 2598-2598/com.mcrlogs.pp.test W/art: Verification of int android.support.v4.app.FragmentActivity.allocateRequestIndex(android.support.v4.app.Fragment) took 330.188ms
01-13 08:00:08.863 2598-2598/com.mcrlogs.pp.test W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-13 08:00:09.315 2598-2712/com.mcrlogs.pp.test I/OpenGLRenderer: Initialized EGL, version 1.4
01-13 08:00:09.315 2598-2712/com.mcrlogs.pp.test D/OpenGLRenderer: Swap behavior 1
01-13 08:00:09.384 2598-2712/com.mcrlogs.pp.test E/EGL_emulation: tid 2712: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-13 08:00:09.385 2598-2712/com.mcrlogs.pp.test W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9a3de140, error=EGL_BAD_MATCH
01-13 08:00:12.178 2598-2712/com.mcrlogs.pp.test E/EGL_emulation: tid 2712: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-13 08:00:12.178 2598-2712/com.mcrlogs.pp.test W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa94f6900, error=EGL_BAD_MATCH
01-13 08:00:12.639 2598-2598/com.mcrlogs.pp.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-13 08:00:17.240 2598-2598/com.mcrlogs.pp.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-13 08:00:17.240 2598-2598/com.mcrlogs.pp.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-13 08:00:25.652 2598-2712/com.mcrlogs.pp.test E/EGL_emulation: tid 2712: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-13 08:00:25.652 2598-2712/com.mcrlogs.pp.test W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x99497ce0, error=EGL_BAD_MATCH
01-13 08:00:26.882 2598-2712/com.mcrlogs.pp.test E/EGL_emulation: tid 2712: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-13 08:00:26.882 2598-2712/com.mcrlogs.pp.test W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x99497ea0, error=EGL_BAD_MATCH
01-13 08:00:27.709 2598-2598/com.mcrlogs.pp.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-13 08:00:31.524 2598-2598/com.mcrlogs.pp.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-13 08:00:31.524 2598-2598/com.mcrlogs.pp.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-13 08:00:31.568 2598-2712/com.mcrlogs.pp.test E/EGL_emulation: tid 2712: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-13 08:00:31.568 2598-2712/com.mcrlogs.pp.test W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x99a463e0, error=EGL_BAD_MATCH
01-13 08:00:31.684 2598-2712/com.mcrlogs.pp.test E/EGL_emulation: tid 2712: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-13 08:00:31.685 2598-2712/com.mcrlogs.pp.test W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x99497ea0, error=EGL_BAD_MATCH
01-13 08:00:38.850 2598-2712/com.mcrlogs.pp.test E/EGL_emulation: tid 2712: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-13 08:00:38.850 2598-2712/com.mcrlogs.pp.test W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x99497ea0, error=EGL_BAD_MATCH
01-13 08:00:42.997 2598-2598/com.mcrlogs.pp.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-13 08:00:48.922 2598-2888/com.mcrlogs.pp.test D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-13 08:00:49.791 2598-2712/com.mcrlogs.pp.test E/EGL_emulation: tid 2712: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-13 08:00:49.791 2598-2712/com.mcrlogs.pp.test W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x994b8ca0, error=EGL_BAD_MATCH
01-13 08:00:49.862 2598-2598/com.mcrlogs.pp.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
答案 0 :(得分:2)
您的用户名值必须为String
,并且您将int
传递给它,所以
改变这个:
mysqli_stmt_bind_param($statement, "siss", $name, $user, $email, $password);
^//error
在PHP
代码中使用此代码:
mysqli_stmt_bind_param($statement, "ssss", $name, $user, $email, $password);