2015-04-08 77 views
2

我有一個Java Servlet後端,數據存儲連接到我的應用程序;我試圖使用Android Studio LoginActivity模板實現一個使用用戶的電子郵件和密碼(不是PlusBaseActivity處理Google帳戶登錄)的登錄系統,但我不知道如何從這裏開始:自定義用戶在AppEngine中登錄

如何才能你說用戶已登錄?以及如何持久使用我的數據存儲庫?我在這裏讀到:How to login User using UserService on AppEngine Java,我只需要調用該方法resp.sendRedirect(userService.createLoginURL(req.getRequestURI())),我已經這樣做了:

@Override 
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
     int size = checkDatastore(); // 0 if empty, > 0 if not empty 
     if(size==0){ 
      populateDatastore(); 
     } 

     String asyncMessage = req.getParameter("order"); 

     if(asyncMessage.equals("login")){ 
      mail = req.getParameter("email"); 
      psw = req.getParameter("password"); 

      UserService userService = UserServiceFactory.getUserService(); 
      User user = userService.getCurrentUser(); 

      String message=""; 
      resp.setContentType("text/plain"); 
      PrintWriter out = resp.getWriter(); 
      if(user == null) { 

      //Sends a temporary redirect response to the client using the 
      // specified redirect location URL and clears the buffer. 
      String uri = userService.createLoginURL(req.getRequestURI()); 
      resp.sendRedirect(uri); 
      User user1 = userService.getCurrentUser(); 
      message="No one is logged in!\n" + "Sent from App Engine at " + new Date(); 

      out.println(message); 
      out.flush(); 
     }if(user !=null) { 
      // login(user); 
      message = "Hello, " + user.getEmail() + 
        ", "+user.getNickname()+"!" + "\nSent from App Engine at "+ new Date(); 

      out.println(message); 
      out.flush(); 
     } 
    } 
} 

sendRedirect()方法只給了我一個URI。做什麼的?

此外,User user = userService.getCurrentUser()總是返回null。怎麼來的?

回答

1

這是因爲UserService的resp.sendRedirect(userService.createLoginURL(req.getRequestURI())) 僅在將登錄與Google帳戶集成時起作用,如this文檔中所示。 如果你想實現一個個性化的登錄系統,你可以通過多種方式來實現。當然,你需要一個Servlet來檢查新用戶的數據和一個數據存儲來持久地存儲新的賬戶註冊信息。