Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android開發連接服務器調用數據庫

Android開發連接服務器調用數據庫

編輯:Android開發實例

  創建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類也要相應的建立。




 

  工程下建立已經封裝好的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. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved