安卓工作室注册不起作用



我正在用安卓工作室为安卓创建一个社交网络。当我在所有字段中输入文本并单击Register没有任何反应时.我之前检查过Android显示器,它打印出我没有使用互联网权限。所以我在.现在我没有收到任何错误,当我单击"注册"时仍然没有任何反应。当我点击注册时,如果注册成功,用户的信息进入数据库,然后他们进入登录屏幕。有人可以帮助我解决这个问题吗?

安卓清单:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

注册活动.java :

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 etPw = (EditText) findViewById(R.id.etPw);
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
final Button bRegister = (Button) findViewById(R.id.bRegister);
final TextView loginLink = (TextView) findViewById(R.id.tvLoginHere);
loginLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(loginIntent);
}
});
bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String username = etUsername.getText().toString();
final String pw = etPw.getText().toString();
final String email = etEmail.getText().toString();
if (etUsername.getText().toString().trim().length() <= 0) {
Toast.makeText(RegisterActivity.this, "Username is empty", Toast.LENGTH_SHORT).show();
}
if (etPw.getText().toString().trim().length() <= 0) {
Toast.makeText(RegisterActivity.this, "Password is empty", Toast.LENGTH_SHORT).show();
}
if (etEmail.getText().toString().trim().length() <= 0) {
Toast.makeText(RegisterActivity.this, "E-mail is empty", Toast.LENGTH_SHORT).show();
}
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, pw, email, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}

注册请求.java :

public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://hash.host22.com/register.php";
private Map<String, String> params;
public RegisterRequest(String username, String pw, String email, Response.Listener<String> listener) {
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("pw", pw);
params.put("email", email);
}
@Override
public Map<String, String> getParams() {
return params;
}
}

注册.php :

if(isset($_POST['bRegister'])) {
if (empty($_POST["username"])) {
echo"Fill in username to sign up";
} else {
if (empty($_POST["pw"])) {
echo"Fill in password to sign up";
} else {
if (empty($_POST["pw2"])) {
echo"Confirm password to sign up";
} else {
if (empty($_POST["email"])) {
echo"Fill in email to sign up";
} else {
if ($_POST['pw'] == $_POST['pw2']) {
$username = mysqli_real_escape_string($con, $_POST["username"]);
$pw= mysqli_real_escape_string($con, $_POST["pw"]);
$email = mysqli_real_escape_string($con, $_POST["email"]);
$result = mysqli_query($con ,"SELECT * FROM users WHERE username='" . $username . "'");
if(mysqli_num_rows($result) > 0)
{
echo "Username exists";
} else {
$result2 = mysqli_query($con ,"SELECT * FROM users WHERE email='" . $email. "'");
if(mysqli_num_rows($result2) > 0)
{
echo "Email exist";
} else {
$pw = password_hash($pw, PASSWORD_BCRYPT, array('cost' => 14));          
$sql = "INSERT INTO users (username, pw, email) VALUES('" . $username . "', '" . $pw . "', '" . $email . "')";
if(mysqli_query($con, $sql)){                                  
// if insert checked as successful echo username and password saved successfully
echo"success";
}else{
echo mysqli_error($con);
}   
} } } else{
echo "The passwords do not match.";  // and send them back to registration page
}}}}}
}

请帮忙谢谢。

我的应用程序上有一个注册活动,它使用与您的类似的一对 java 文件(registerActivity 和 registerRequest(,但对于我的 php 文件,我使用了更简单的东西。我将在下面发布它,如果您愿意或调整自己的代码以适应它,请尝试一下,但是使用 000webhost 数据库对我来说,它大部分时间都有效。

$con = mysqli_connect("host", "username", "password", "db_name");
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$response = array();
$response["success"] = 0;  
$email_check = mysqli_query($con, "SELECT * FROM tableName WHERE email = '$email'");
if(mysqli_num_rows($email_check)) {
$response["success"] = 1;
echo json_encode($response);
exit;
}
$statementR = mysqli_prepare($con, "INSERT INTO tableName (name, email, password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statementR, "sss", $name, $email, $password);
mysqli_stmt_execute($statementR);

$statement = mysqli_prepare($con, "SELECT * FROM tableName WHERE email = ?");
mysqli_stmt_bind_param($statement, "s", $email);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $user_id, $name, $email, $password);
while(mysqli_stmt_fetch($statement)){
$response["success"] = 2;  
$response["user_id"] = $user_id;    
$response["name"] = $name;
$response["email"] = $email;
}
echo json_encode($response);

email_check 方法检查电子邮件是否已存在,然后返回带有整数值的响应,然后应用将解释该响应。

我的 php 脚本中没有任何密码/用户名检查,因为这都是在应用程序端完成的,因为它可以防止不必要的数据库调用(如果您愿意,我也可以发布该代码(。如果需要,您可以将服务器端数据检查添加到上面的代码中。

为您修改的寄存器.php文件:

$con = mysqli_connect("host", "username", "password", "db_name");
$name = $_POST["username"];
$email = $_POST["email"];
$pw = $_POST["pw"];
$response = array();
$response["success"] = 0;  
$email_check = mysqli_query($con, "SELECT * FROM users WHERE email = '$email'");
if(mysqli_num_rows($email_check)) {
$response["success"] = 1;
echo json_encode($response);
exit;
}
$statementR = mysqli_prepare($con, "INSERT INTO users (username, pw, email) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statementR, "sss", $username, $pw, $email);
mysqli_stmt_execute($statementR);

$statement = mysqli_prepare($con, "SELECT * FROM users WHERE email = ?");
mysqli_stmt_bind_param($statement, "s", $email);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $username, $pw, $email);
while(mysqli_stmt_fetch($statement)){
$response["success"] = 2;  
$response["username"] = $username;    
$response["pw"] = $pw;
$response["email"] = $email;
}
echo json_encode($response);

同样在注册活动中,将行if(success){...}更新为if(success == 2){...}

为了清楚起见,这是我的注册活动(与您的非常相似(:

public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText registerName = (EditText) findViewById(R.id.registerName);
final EditText registerEmail = (EditText) findViewById(R.id.registerEmail);
final EditText registerPassword = (EditText) findViewById(R.id.registerPassword);
final EditText registerConfirm = (EditText) findViewById(R.id.registerPasswordConfirm);
final Button registerButton = (Button) findViewById(R.id.registerButton);
final TextView registerLogInLink = (TextView) findViewById(R.id.registerLogInLink);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name = registerName.getText().toString();
final String email = registerEmail.getText().toString();
final String password = registerPassword.getText().toString();
final String passwordCheck = registerConfirm.getText().toString();
if(name.length() > 1 & email.length() > 5 & password.length() > 5 & password.equals(passwordCheck)) {
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
System.out.println(response);
JSONObject jsonResponse = new JSONObject(response);
int success = jsonResponse.getInt("success");
System.out.println(jsonResponse);
System.out.println(success);
if (success == 2) {
Integer user_id = jsonResponse.getInt("user_id");
String name = jsonResponse.getString("name");
String email = jsonResponse.getString("email");
UserCredentials.setUserLoggedInStatus(getApplicationContext(), true);
UserCredentials.setLoggedInUserEmail(getApplicationContext(), email);
UserCredentials.setLoggedInUserName(getApplicationContext(), name);
UserCredentials.setLoggedInUserID(getApplicationContext(), user_id);
Intent intent = new Intent(RegisterActivity.this, MainScreen.class);
RegisterActivity.this.startActivity(intent);
} else if (success == 1) {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Registration failed, email already registered.")
.setNegativeButton("Try again", null)
.create()
.show();
} else {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Registration failed, please try again")
.setNegativeButton("Try again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
RegisterActivity.this.recreate();}})
.create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, email, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
} else if(name.length() > 1 & email.length() <= 5 & password.length() <=5) {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Registration failed, email and password invalid. Passwords must be more than 5 characters.")
.setNegativeButton("Retry", null)
.create()
.show();
} else if(name.length() > 1 & email.length() <= 5 & password.length() > 5) {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Registration failed, email invalid.")
.setNegativeButton("Retry", null)
.create()
.show();
} else if(name.length() > 1 & email.length() > 5 & password.length() <= 5) {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Registration failed, invalid password. Passwords must be more than 5 characters.")
.setNegativeButton("Retry", null)
.create()
.show();
} else if(name.length() <= 1 & email.length() <= 5 & password.length() <=5) {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Registration failed, all credentials invalid.")
.setNegativeButton("Retry", null)
.create()
.show();
} else if(name.length() <= 1 & email.length() <= 5 & password.length() > 5) {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Registration failed, name and email invalid.")
.setNegativeButton("Retry", null)
.create()
.show();
} else if(name.length() <= 1 & email.length() > 5 & password.length() <= 5) {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Registration failed, name and password invalid. Passwords must be more than 5 characters.")
.setNegativeButton("Retry", null)
.create()
.show();
}  else if(name.length() > 1 & email.length() > 5 & password.length() > 5 & !password.equals(passwordCheck)) {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Registration failed, passwords do not match.")
.setNegativeButton("Retry", null)
.create()
.show();
} else {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage("Something went wrong, please try again.")
.setNegativeButton("Retry", null)
.create()
.show();
}
}
});
registerLogInLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent LogInIntent = new Intent(RegisterActivity.this, LogInActivity.class);
RegisterActivity.this.startActivity(LogInIntent);
}
});

您可以忽略 alertDialog,除非您需要自定义警报,在这种情况下,请随意复制它们。还要确保您的 RegisterRequest 文件连接到正确的 URL。

最新更新