![](/img/trans.png)
[英]App stops working when I pressed the sign up button in Android Studio
[英]Android studio sign up not working
我正在使用android studio为android创建社交网络。 当我在所有字段中输入文本并单击Register
没有任何反应。 我之前检查了Android Monitor并打印出我没有使用Internet权限。 所以我在中添加了它。 现在,我没有收到任何错误,单击“注册”后仍然没有任何反应。 当我单击注册时,如果注册成功,则用户的信息进入数据库,然后他们进入登录屏幕。 有人可以帮我解决这个问题吗?
Android清单:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
registeractivity.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);
}
});
}
}
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;
}
}
register.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 db对我而言,它的工作时间为100%。
$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脚本中没有任何密码/用户名检查,因为这一切都在应用程序端完成,因为它可以防止不必要的数据库调用(如果您愿意,我也可以发布该代码)。 如果愿意,您可以将服务器端数据检查添加到上面的代码中。
为您修改了register.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);
同样在您的RegisterActivity中,将if(success){...}
行更新为if(success == 2){...}
。
为了清楚起见,这是我的RegisterActivity(与您的非常相似):
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);
}
});
您可以忽略alertDialogs,除非您需要自定义警报,在这种情况下,可以随意复制它们。 还要确保您的RegisterRequest文件正在连接到正确的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.