Java Android studio注册不起作用
我正在用android studio为android创建一个社交网络。当我在所有字段中输入文本并单击“注册”时,什么也没有发生。我之前检查过安卓显示器,它显示我没有使用互联网许可。所以我把这个加进去了。现在我没有收到任何错误,当我单击Register时仍然没有发生任何事情。当我单击Register时,如果注册成功,用户的信息将进入数据库,然后进入登录屏幕。有人能帮我解决这个问题吗 Android清单:Java Android studio注册不起作用,java,php,android,performance,Java,Php,Android,Performance,我正在用android studio为android创建一个社交网络。当我在所有字段中输入文本并单击“注册”时,什么也没有发生。我之前检查过安卓显示器,它显示我没有使用互联网许可。所以我把这个加进去了。现在我没有收到任何错误,当我单击Register时仍然没有发生任何事情。当我单击Register时,如果注册成功,用户的信息将进入数据库,然后进入登录屏幕。有人能帮我解决这个问题吗 Android清单: <uses-permission android:name="android.permi
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
请帮助,谢谢。我的应用程序上有一个注册活动,它使用了一对与您类似的java文件(registerActivity和registerRequest),但对于我的php文件,我使用了一些更简单的东西。我会把它贴在下面,如果你想试试,或者修改你自己的代码来适应它,但是我使用000WebHostDB时,它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(与您的非常相似):
公共类注册表活动扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u寄存器);
最终EditText注册表名=(EditText)findViewById(R.id.registerName);
final EditText registerEmail=(EditText)findViewById(R.id.registerEmail);
最终的EditText寄存器Password=(EditText)findViewById(R.id.registerPassword);
最终EditText注册表确认=(EditText)findViewById(R.id.registerPasswordConfirm);
最终按钮寄存器按钮=(按钮)findViewById(R.id.registerButton);
最终TextView registerLogInLink=(TextView)findViewById(R.id.registerLogInLink);
registerButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
最终字符串名称=registerName.getText().toString();
最终字符串email=registerEmail.getText().toString();
最终字符串密码=registerPassword.getText().toString();
final String passwordCheck=RegisterConfig.getText().toString();
if(name.length()>1&email.length()>5&password.length()>5&password.equals(密码检查)){
Response.Listener responseListener=新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
System.out.println(响应);
JSONObject jsonResponse=新的JSONObject(响应);
int success=jsonResponse.getInt(“success”);
System.out.println(jsonResponse);
System.out.println(成功);
如果(成功==2){
整数user_id=jsonResponse.getInt(“user_id”);
String name=jsonResponse.getString(“name”);
String email=jsonResponse.getString(“电子邮件”);
UserCredentials.setUserLoggedInStatus(getApplicationContext(),true);
setLoggedInUserEmail(getApplicationContext(),email);
UserCredentials.setLoggedInUserName(getApplicationContext(),name);
UserCredentials.setLoggedInUserID(getApplicationContext(),用户id);
意向意向=新意向(RegisterActivity.this,MainScreen.class);
RegisterActivity.this.startActivity(意向);
}else if(成功==1){
AlertDialog.Builder alertMessage=新建AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage(“注册失败,电子邮件已注册。”)
.setNegativeButton(“重试”,null)
.create()
.show();
}否则{
AlertDialog.Builder alertMessage=新建AlertDialog.Builder(RegisterActivity.this);
alertMessage.setMessage(“注册失败,请重试”)
.setNegativeButton(“重试”,新建DialogInterface.OnClickListener()){
public void onClick(DialogInterface对话框,int-id){
RegisterActivity.this.recreate();})
.create().show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
};
RegisterRequest RegisterRequest=新的RegisterRequest(名称、电子邮件、密码、响应侦听器);
RequestQueue=Volley.newRequestQueue(RegisterActivity.this);
添加(registerRequest);
}else if(name.length()>1&email.length()1&email.length()>5&password.length()我在我的应用程序上有一个注册活动,它使用与你类似的一对java文件(registerActivity和registerRequest),但对于我的php文件,我使用了一些更简单的东西。我将在下面发布,如果你想试试,或者修改自己的代码以适应它,但它可以100%工作
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
}}}}}
}
$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);
$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);
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);
}
});