创建RegActivity
客户端建立:
HttpURL url = new HttpURL();
// url地址
String requestUrl = "http://10.0.2.2:8080/web/servlet/RegServlet";
// 参数,跟数据库传入数据结合,蓝色字相当于哈希表索引,可以随便定义,无实际意义,但要和客户端接口实现相同命名
Map<String, String> requestParams = new HashMap<String, String>();
requestParams.put("userName", loginName);
requestParams.put("userPass", pass);
requestParams.put("sex", sex);
requestParams.put("city", ct);
requestParams.put("job", job);
requestParams.put("marry", ma);
requestParams.put("love", love);
// 获取登录用户信息,定死的
InputStream ins = url.getHttpCon(requestUrl, requestParams);
XStream xStream = new XStream();
UserInfo user = (UserInfo)xStream.fromXML(ins);
url中”servlet/RegServlet”为获得的
客户端上网条件设置:
在相同界面添加Activity页面注册:(最标准的情况应该在创建RegActivity时,同时创建)
在服务器和客户端分别建立相同的包,并在包下建立相同的接口类
工程下建立已经封装好的HttpURL
服务器下建立与RegActivity相应的RegServlet连接
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//账号
String username = request.getParameter("userName");
//密码
String userpass = request.getParameter("userPass");
String sex = request.getParameter("sex");
String city = request.getParameter("city");
String job = request.getParameter("job");
String marrage = request.getParameter("marry");
String love = request.getParameter("love");
//数据访问
UserInfoDAO dao = new UserInfoDAO();
//创建user
UserInfo userInfo = new UserInfo();
userInfo.setUserName(username);
userInfo.setUserPass(userpass);
userInfo.setSex(sex);
userInfo.setCity(city);
userInfo.setJob(job);
userInfo.setMarry(marrage);
userInfo.setLove(love);
UserInfo user = dao.add(userInfo);
UserInfo user1 = dao.reId(user);
//生成xml
XStream xStream = new XStream();
System.out.println(xStream.toXML(user1));
PrintWriter out = response.getWriter();
out.print(xStream.toXML(user1));
out.flush();
out.close();
}
上图与数据库交互的Dao类也要相应的建立。
常见问题:
1.数据库已经可以显示,但输入信息不全:客户和服务器哈希表索引不一致。
2.LogCat进行调试,控制台调试,System.out.println();,Toast调试
3.注意客户端上网允许条件。