用androidstudio写一个可以和springboot连接的登录注册系统
时间: 2024-02-20 19:02:28 浏览: 218
好的,下面是一个简单的Android Studio登录注册系统的实现步骤:
1. 创建一个新的Android Studio项目,选择Empty Activity。
2. 在app/build.gradle文件中添加以下依赖项:
```
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.android.support:design:28.0.0'
```
3. 在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.INTERNET" />
```
4. 在res/layout文件夹中创建activity_login.xml和activity_register.xml布局文件,分别用于登录和注册界面。
5. 在MainActivity.java中,实现登录和注册方法,代码如下:
```java
public class MainActivity extends AppCompatActivity {
private EditText etUsername, etPassword;
private Button btnLogin, btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
btnLogin = findViewById(R.id.btn_login);
btnRegister = findViewById(R.id.btn_register);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
login(username, password);
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
private void login(final String username, final String password) {
String url = "http://your_server_url/login.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equals("success")) {
Toast.makeText(MainActivity.this, "Login Success!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Login Failed!", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("username", username);
params.put("password", password);
return params;
}
};
Volley.newRequestQueue(this).add(stringRequest);
}
}
```
6. 在res/layout文件夹中创建activity_register.xml布局文件,用于注册界面。
7. 在RegisterActivity.java中,实现注册方法,代码如下:
```java
public class RegisterActivity extends AppCompatActivity {
private EditText etUsername, etPassword, etConfirmPassword;
private Button btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
etConfirmPassword = findViewById(R.id.et_confirm_password);
btnRegister = findViewById(R.id.btn_register);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
String confirmPassword = etConfirmPassword.getText().toString();
if (password.equals(confirmPassword)) {
register(username, password);
} else {
Toast.makeText(RegisterActivity.this, "Passwords do not match!", Toast.LENGTH_SHORT).show();
}
}
});
}
private void register(final String username, final String password) {
String url = "http://your_server_url/register.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equals("success")) {
Toast.makeText(RegisterActivity.this, "Registration Success!", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(RegisterActivity.this, "Registration Failed!", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RegisterActivity.this, "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("username", username);
params.put("password", password);
return params;
}
};
Volley.newRequestQueue(this).add(stringRequest);
}
}
```
8. 在服务器端实现登录和注册接口,示例代码如下:
```php
<?php
$conn = mysqli_connect("localhost", "username", "password", "database");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
$password = hash('sha256', $password);
$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "success";
} else {
echo "failure";
}
}
mysqli_close($conn);
?>
<?php
$conn = mysqli_connect("localhost", "username", "password", "database");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
$password = hash('sha256', $password);
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if (mysqli_query($conn, $sql)) {
echo "success";
} else {
echo "failure";
}
}
mysqli_close($conn);
?>
```
注意:这只是一个简单的示例,实际应用中需要进行更多的安全性检查和验证。
阅读全文