给出android studio登录注册界面的客户端和服务端代码例子
时间: 2023-11-06 15:08:28 浏览: 151
很抱歉,我不能提供完整的客户端和服务端代码,因为这需要考虑到很多因素,例如您的项目需求、数据存储方式等等。但是,我可以给您提供一些参考的例子来帮助您开始编写自己的代码。
客户端代码:
1. 布局文件:login_activity.xml
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:hint="Username"
android:inputType="text"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/username_edittext"
android:layout_marginTop="20dp"
android:hint="Password"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/password_edittext"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Login" />
<Button
android:id="@+id/register_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/login_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Register" />
</RelativeLayout>
```
2. Java代码:LoginActivity.java
```java
public class LoginActivity extends AppCompatActivity {
private EditText mUsernameEditText;
private EditText mPasswordEditText;
private Button mLoginButton;
private Button mRegisterButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
mUsernameEditText = findViewById(R.id.username_edittext);
mPasswordEditText = findViewById(R.id.password_edittext);
mLoginButton = findViewById(R.id.login_button);
mRegisterButton = findViewById(R.id.register_button);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mUsernameEditText.getText().toString();
String password = mPasswordEditText.getText().toString();
login(username, password);
}
});
mRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
private void login(String username, String password) {
// 创建HTTP连接
HttpURLConnection connection = null;
try {
URL url = new URL("http://your-server-url.com/login");
connection = (HttpURLConnection) url.openConnection();
// 设置请求方法和超时时间
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
// 设置请求参数
String body = "username=" + username + "&password=" + password;
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(body.getBytes());
outputStream.flush();
outputStream.close();
// 解析响应代码和响应数据
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
// 登录成功
InputStream inputStream = connection.getInputStream();
String responseData = StreamUtils.inputStreamToString(inputStream);
JSONObject jsonObject = new JSONObject(responseData);
String userId = jsonObject.getString("userId");
String token = jsonObject.getString("token");
// 存储用户ID和Token
SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("userId", userId);
editor.putString("token", token);
editor.apply();
// 跳转到主界面
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
// 登录失败
Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
```
服务端代码:
1. Java代码:LoginServlet.java
```java
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 查询数据库,检查用户名和密码是否正确
User user = UserDao.getUserByUsername(username);
if (user == null || !user.getPassword().equals(password)) {
// 用户名或密码不正确,返回401响应代码
response.setStatus(401);
return;
}
// 生成Token
String token = TokenUtils.generateToken();
// 将Token存储到数据库中
UserDao.updateToken(user.getId(), token);
// 构造响应JSON数据
JSONObject jsonObject = new JSONObject();
jsonObject.put("userId", user.getId());
jsonObject.put("token", token);
// 发送响应数据
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(jsonObject.toString());
out.flush();
out.close();
}
}
```
2. Java代码:UserDao.java
```java
public class UserDao {
public static User getUserByUsername(String username) {
// 查询数据库,根据用户名获取用户信息
// ...
return user;
}
public static void updateToken(int userId, String token) {
// 更新数据库,将Token存储到用户表中
// ...
}
}
```
3. Java代码:TokenUtils.java
```java
public class TokenUtils {
public static String generateToken() {
// 生成Token的逻辑
// ...
return token;
}
}
```
以上代码仅供参考,您需要根据自己的实际需求进行修改和完善。
阅读全文