其他分享
首页 > 其他分享> > 安卓后台开发1

安卓后台开发1

作者:互联网

Android 开发后台在这里插入图片描述
在servlet包下面创建LoginDateServlet 服务

package com.hxb.servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.hxb.db.DBUtils;
import com.hxb.domain.BaseBean;
import com.hxb.domain.UserBean;
public class LoginDateServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(“request—>”+request.getRequestURL()+"===="+request.getParameterMap().toString());
String username = request.getParameter(“username”); // 获取客户端传过来的参数
String password = request.getParameter(“password”);
response.setContentType(“text/html;charset=utf-8”);
if (username == null || username.equals("") || password == null || password.equals("")) {
System.out.println(“用户名或密码为空”);
return;
} // 请求数据库
DBUtils dbUtils = new DBUtils();
dbUtils.openConnect();
// 打开数据库连接
BaseBean data = new BaseBean(); // 基类对象,回传给客户端的json对象
UserBean userBean = new UserBean(); // user的对象
if (dbUtils.isExistInDB(username, password)) {
// 判断账号是否存在
data.setCode(-1);
data.setData(userBean);
data.setMsg(“该账号已存在”);
} else if (!dbUtils.insertDataToDB(username, password)) {
// 注册成功
data.setCode(0);
data.setMsg(“注册成功!!”);
ResultSet rs = dbUtils.getUser();
int id = -1;
if (rs != null) {
try {
while (rs.next()) {
if (rs.getString(“user_name”).equals(username)
&& rs.getString(“user_pwd”).equals(password)) {
id = rs.getInt(“user_id”);
}
}
userBean.setId(id);
} catch (SQLException e) {
e.printStackTrace();
}
}
userBean.setUsername(username);
userBean.setPassword(password);
data.setData(userBean);
} else {
// 注册不成功,这里错误没有细分,都归为数据库错误
data.setCode(500);
data.setData(userBean);
data.setMsg(“数据库错误”);
}
Gson gson = new Gson();
String json = gson.toJson(data);
// 将对象转化成json字符串
try {
response.getWriter().println(json);
// 将json数据传给客户端
} catch (Exception e) {
e.printStackTrace();
} finally {
response.getWriter().close(); // 关闭这个流,不然会发生错误的
}
dbUtils.closeConnect(); // 关闭数据库连接}
}
}
接下来在Android 上面布局应用

<?xml version="1.0" encoding="utf-8"?>
<EditText
    android:id="@+id/et_data_uname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入用户名:" />

<EditText
    android:id="@+id/et_data_upass"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入密码:"
    android:inputType="number" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="loginGET"
    android:text="登录(GET)" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="loginPOST"
    android:text="登录(POST)" />

标签:username,安卓,request,userBean,开发,后台,import,password,data
来源: https://blog.csdn.net/weizhishude/article/details/114170990