android端
// 验证登录信息
public String queryLogin(String inputUsername, String inputPwd) {
String url = "http://192.168.1.105:8080/epay_server/LoginServlet?username="
+ inputUsername + "&pwd=" + inputPwd;
// 发送请求
HttpPost request = HttpUtil.getHttpPost(url);
// 接受响应
HttpResponse response = HttpUtil.getHttpResponse(request);
// 返回结果
return HttpUtil.getValue(response);
}
// 获取post
public static HttpPost getHttpPost(String url) {
HttpPost request = new HttpPost(url);
return request;
}
// post响应
public static HttpResponse getHttpResponse(HttpPost request) {
try {
System.out.println("得到的request==>" + request);
HttpResponse response = new DefaultHttpClient().execute(request);
System.out.println("得到的response==>" + response);
return response;
} catch (ClientProtocolException e) {
System.out.println("clientProtocol异常");
e.printStackTrace();
return null;
} catch (IOException e) {
System.out.println("io异常");
e.printStackTrace();
return null;
}
}
// 获取页面信息
public static String getValue(HttpResponse response) {
String result = "";
if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
// 打印页面信息
try {
result = EntityUtils.toString(response.getEntity());
System.out.println("页面信息==>" + result);
} catch (ParseException e) {
System.out.println("ParseException异常");
e.printStackTrace();
} catch (IOException e) {
System.out.println("io异常...");
e.printStackTrace();
}
} else {
System.out.println("null...");
}
return result;
}
点击登录按钮:
// 获取输入框的值
String inputUsername = etUsername.getText().toString()
.trim();
String inputPwd = etPwd.getText().toString().trim();
// connectNet.start(); // 开启线程连接网络
String userID = queryLogin(inputUsername, inputPwd);
System.out.println("id===>" + userID);
if (!userID.equals("")) {
Toast.makeText(mContext, "登录成功!", Toast.LENGTH_SHORT)
.show();
// 存储id
editor.putString(GlobalConstant.SHA_USER_ID, userID);
// 提交存入数据库
editor.commit();
Intent intent = new Intent(mContext, MainActivity.class);
startActivity(intent);
cleanEditText(); // 清空登陆框
} else {
Toast.makeText(mContext, "用户名或密码错误!",
Toast.LENGTH_SHORT).show();
}
服务器端:
dao.java:
/**
* 数据库操作的接口:增删改查
* @author smalt
*
*/
public interface DBDao {
public void getUpdate();
}
daoImpl:
public class LoginDaoImpl implements LoginDao {
public Login getLogin(String username, String password) {
// 从数据库中查找用户名密码,有责显示,否则返回空
Login login = null;
// 使用MD5加密明文密码
String pwdMD5=MD5Util.getMD5_Fouction1(password);
// 连接数据库
DBUtil db = new DBUtil();
Connection con = db.getConnectionInformix();
if (con != null) {
try { // 查询语句
String sql = "select login,password from ec_pcuser where login=? and password=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, pwdMD5);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
login = new Login();
login.setUsername(username);
login.setPwd(password);
}
if (login!=null) {
System.out.println("username=" + login.getUsername() + ",pwd="
+ login.getPwd());
}else {
System.out.println("null...");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("sql查询异常!");
} finally {
db.closeConnection(con);
}
}
// 返回结果
return login;
}
Servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=GBK");
// 请求的数据
// http://192.168.1.105:8080/epay_server/LoginServlet?username=pc&pwd=pc
String userName = request.getParameter("username");
String userPwd = request.getParameter("pwd");
PrintWriter out = response.getWriter();
DBQueryImpl db = new DBQueryImpl();
// 根据用户名密码查找id,找到返回值,否则返回空
String result = db.getID(userName, userPwd);
out.write(result);
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response); // 验证用户名密码所以和doGet()方法一样...
}