Android用户登录

来源:互联网 发布:网上寺院软件下载 编辑:程序博客网 时间:2024/06/05 16:06

前几天和教我们计算机网络的老师讨论了一些关于手机应用发展前景的问题,学到了很多,有技术方面的,也有非技术方面的,他的观点就是手机网络游戏的发展大势所趋,即使目前技术还不够成熟.

 

所以说Android会有越来越多涉及到后台交互的应用出现,既然要交互,显然登陆是第一步,今天做了下手机登陆,服务器验证用户名密码,还蛮顺利,贴下代码:

 

Server端,我用的tomcat:

Java代码 
  1. package com.action.userAction;  
  2.   
  3. import java.io.OutputStream;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7.   
  8. import org.apache.struts2.ServletActionContext;  
  9.   
  10. import com.opensymphony.xwork2.Action;  
  11.   
  12. public class LoginCheckAction implements Action{  
  13.       
  14.     private String userName;  
  15.     private String password;  
  16.       
  17.     public String execute() throws Exception {  
  18.         // TODO Auto-generated method stub  
  19.           
  20.         HttpServletRequest request = ServletActionContext.getRequest();  
  21.           
  22.         userName = request.getParameter("userName");  
  23.         password = request.getParameter("password");  
  24.       
  25.           
  26.         HttpServletResponse response = ServletActionContext.getResponse();  
  27.           
  28.         OutputStream os = response.getOutputStream();    
  29.           
  30.         if(!"lovehui".equals(userName) || !"1989".equals(password))  
  31.         {  
  32.             os.write(new Integer(-1));  
  33.         }  
  34.         else  
  35.         {  
  36.             os.write(new Integer(1));  
  37.         }  
  38.           
  39.         response.setStatus(HttpServletResponse.SC_OK);  
  40.                
  41.         return null;  
  42.   
  43.     }  
  44.   
  45.     public String getUserName() {  
  46.         return userName;  
  47.     }  
  48.   
  49.     public void setUserName(String userName) {  
  50.         this.userName = userName;  
  51.     }  
  52.   
  53.     public String getPassword() {  
  54.         return password;  
  55.     }  
  56.   
  57.     public void setPassword(String password) {  
  58.         this.password = password;  
  59.     }  
  60.       
  61.   
  62. }  

 

 

接下来是Client端,就贴最主要的一个Activity,有一点要注意下,就是本地回路地址不是常用的127.0.0.1,要换成10.0.2.2,原因也无法赘述,也是借鉴了网上别人的经验:

Java代码 
  1. package src.siwi.map.android;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.NET.HttpURLConnection;  
  5. import java.net.URL;  
  6.   
  7. import android.app.Activity;  
  8. import android.app.AlertDialog;  
  9. import android.app.ProgressDialog;  
  10. import android.content.DialogInterface;  
  11. import android.content.Intent;  
  12. import android.content.SharedPreferences;  
  13. import android.os.Bundle;  
  14. import android.os.Handler;  
  15. import android.os.Message;  
  16. import android.util.Log;  
  17. import android.view.Menu;  
  18. import android.view.MenuItem;  
  19. import android.view.View;  
  20. import android.view.View.OnClickListener;  
  21. import android.widget.Button;  
  22. import android.widget.CheckBox;  
  23. import android.widget.CompoundButton;  
  24. import android.widget.EditText;  
  25. import android.widget.Toast;  
  26. import android.widget.CompoundButton.OnCheckedChangeListener;  
  27.   
  28. public class Login extends Activity {  
  29.     private String userName;  
  30.     private String password;  
  31.       
  32.     private EditText view_userName;  
  33.     private EditText view_password;  
  34.     private CheckBox view_rememberMe;  
  35.     private Button view_loginSubmit;  
  36.       
  37.     private static final int MENU_EXIT = Menu.FIRST - 1;  
  38.     private static final int MENU_ABOUT = Menu.FIRST;  
  39.       
  40.     private final String SHARE_LOGIN_TAG = "MAP_SHARE_LOGIN_TAG";  
  41.   
  42.     private String SHARE_LOGIN_USERNAME = "MAP_LOGIN_USERNAME";  
  43.     private String SHARE_LOGIN_PASSWORD = "MAP_LOGIN_PASSWORD";  
  44.       
  45.     private boolean isNetError;  
  46.       
  47.     private ProgressDialog proDialog;  
  48.   
  49.       
  50.     Handler loginHandler = new Handler() {  
  51.         public void handleMessage(Message msg)   
  52.         {  
  53.             isNetError = msg.getData().getBoolean("isNetError");  
  54.             if(proDialog != null)   
  55.             {  
  56.                 proDialog.dismiss();  
  57.             }  
  58.             if(isNetError)   
  59.             {  
  60.                 Toast.makeText(Login.this"当前网络不可用",  
  61.                         Toast.LENGTH_SHORT).show();  
  62.             }  
  63.             else   
  64.             {  
  65.                 Toast.makeText(Login.this"错误的用户名或密码",  
  66.                         Toast.LENGTH_SHORT).show();  
  67.                   
  68.                 clearSharePassword();  
  69.             }  
  70.         }  
  71.     };  
  72.   
  73.   
  74.     public void onCreate(Bundle savedInstanceState) {  
  75.         super.onCreate(savedInstanceState);  
  76.         setContentView(R.layout.login);  
  77.         findViewsById();  
  78.         initView(false);  
  79.           
  80.         setListener();  
  81.     }  
  82.   
  83.   
  84.     private void findViewsById() {  
  85.         view_userName = (EditText)findViewById(R.id.loginUserNameEdit);  
  86.         view_password = (EditText)findViewById(R.id.loginPasswordEdit);  
  87.         view_rememberMe = (CheckBox)findViewById(R.id.loginRememberMeCheckBox);  
  88.         view_loginSubmit = (Button)findViewById(R.id.loginSubmit);  
  89.     }  
  90.   
  91.       
  92.     private void initView(boolean isRememberMe) {  
  93.         SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);  
  94.         String userName = share.getString(SHARE_LOGIN_USERNAME, "");  
  95.         String password = share.getString(SHARE_LOGIN_PASSWORD, "");  
  96.         Log.d(this.toString(), "userName=" + userName + " password=" + password);  
  97.         if (!"".equals(userName)) {  
  98.             view_userName.setText(userName);  
  99.         }  
  100.         if (!"".equals(password)) {  
  101.             view_password.setText(password);  
  102.             view_rememberMe.setChecked(true);  
  103.         }  
  104.           
  105.         if (view_password.getText().toString().length() > 0) {  
  106.             // view_loginSubmit.requestFocus();  
  107.             // view_password.requestFocus();  
  108.         }  
  109.         share = null;  
  110.     }  
  111.   
  112.       
  113.     private boolean validateLocalLogin(String userName, String password, String validateUrl) {  
  114.           
  115.         boolean loginState = false;  
  116.         HttpURLConnection conn = null;  
  117.         DataInputStream dis = null;  
  118.         try   
  119.         {  
  120.             URL url = new URL(validateUrl);  
  121.             conn = (HttpURLConnection)url.openConnection();  
  122.             conn.setConnectTimeout(5000);  
  123.             conn.setRequestMethod("GET");     
  124.             conn.connect();  
  125.           
  126.             dis = new DataInputStream(conn.getInputStream());  
  127.             if(conn.getResponseCode() != HttpURLConnection.HTTP_OK)   
  128.             {  
  129.                 Log.d(this.toString(), "HTTP ERROR");  
  130.                 isNetError = true;  
  131.                 return false;  
  132.             }  
  133.               
  134.             int loginStateInt = dis.read();  
  135.   
  136.             Log.v("loginState", String.valueOf(loginStateInt));  
  137.             if(loginStateInt == 1)   
  138.             {  
  139.                 loginState = true;  
  140.             }  
  141.         }   
  142.         catch (Exception e)   
  143.         {  
  144.             e.printStackTrace();  
  145.             isNetError = true;  
  146.             Log.d(this.toString(), e.getMessage() + "  127 line");  
  147.         }   
  148.         finally   
  149.         {  
  150.             if(conn != null)   
  151.             {  
  152.                 conn.disconnect();  
  153.             }  
  154.         }  
  155.           
  156.         if(loginState)   
  157.         {  
  158.             if(isRememberMe())   
  159.             {  
  160.                 saveSharePreferences(truetrue);  
  161.             }   
  162.             else   
  163.             {  
  164.                 saveSharePreferences(truefalse);  
  165.             }  
  166.         }   
  167.         else   
  168.         {  
  169.             if(!isNetError)  
  170.             {  
  171.                 clearSharePassword();  
  172.             }  
  173.         }  
  174.         if(!view_rememberMe.isChecked())   
  175.         {  
  176.             clearSharePassword();  
  177.         }  
  178.           
  179.         Log.v("loginState", String.valueOf(loginState));  
  180.         return loginState;  
  181.     }  
  182.   
  183.       
  184.     private void saveSharePreferences(boolean saveUserName, boolean savePassword) {  
  185.         SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);  
  186.         if(saveUserName) {  
  187.             Log.d(this.toString(), "saveUserName="  
  188.                     + view_userName.getText().toString());  
  189.             share.edit().putString(SHARE_LOGIN_USERNAME,  
  190.                     view_userName.getText().toString()).commit();  
  191.         }  
  192.         if (savePassword) {  
  193.             share.edit().putString(SHARE_LOGIN_PASSWORD,  
  194.                     view_password.getText().toString()).commit();  
  195.         }  
  196.         share = null;  
  197.     }  
  198.   
  199.       
  200.     private boolean isRememberMe() {  
  201.         if (view_rememberMe.isChecked()) {  
  202.             return true;  
  203.         }  
  204.         return false;  
  205.     }  
  206.   
  207.       
  208.     private OnClickListener submitListener = new OnClickListener() {  
  209.       
  210.         public void onClick(View v) {  
  211.             proDialog = ProgressDialog.show(Login.this"请稍候",  
  212.                     ""truetrue);  
  213.               
  214.             Thread loginThread = new Thread(new LoginFailureHandler());  
  215.             loginThread.start();  
  216.         }  
  217.     };  
  218.   
  219.     // .start();  
  220.     // }  
  221.     // };  
  222.   
  223.       
  224.     private OnCheckedChangeListener rememberMeListener = new OnCheckedChangeListener() {  
  225.   
  226.   
  227.         public void onCheckedChanged(CompoundButton buttonView,  
  228.                 boolean isChecked) {  
  229.             if (view_rememberMe.isChecked()) {  
  230.                 Toast.makeText(Login.this"ischecked",  
  231.                         Toast.LENGTH_SHORT).show();  
  232.             }  
  233.         }  
  234.     };  
  235.   
  236.   
  237.       
  238.   
  239.       
  240.     private void setListener() {  
  241.         view_loginSubmit.setOnClickListener(submitListener);  
  242.           
  243.         view_rememberMe.setOnCheckedChangeListener(rememberMeListener);  
  244.     }  
  245.   
  246.       
  247.     public boolean onCreateOptionsMenu(Menu menu) {  
  248.         super.onCreateOptionsMenu(menu);  
  249.         menu.add(0, MENU_EXIT, 0, getResources().getText(R.string.MENU_EXIT));  
  250.         menu.add(0, MENU_ABOUT, 0, getResources().getText(R.string.MENU_ABOUT));  
  251.         return true;  
  252.     }  
  253.   
  254.   
  255.     public boolean onMenuItemSelected(int featureId, MenuItem item) {  
  256.         super.onMenuItemSelected(featureId, item);  
  257.         switch (item.getItemId()) {  
  258.         case MENU_EXIT:  
  259.             finish();  
  260.             break;  
  261.         case MENU_ABOUT:  
  262.             alertAbout();  
  263.             break;  
  264.         }  
  265.         return true;  
  266.     }  
  267.   
  268.       
  269.     private void alertAbout() {  
  270.         new AlertDialog.Builder(Login.this).setTitle(R.string.MENU_ABOUT)  
  271.                 .setMessage(R.string.aboutInfo).setPositiveButton(  
  272.                         R.string.ok_label,  
  273.                         new DialogInterface.OnClickListener() {  
  274.                             public void onClick(  
  275.                                     DialogInterface dialoginterface, int i) {  
  276.                             }  
  277.                         }).show();  
  278.     }  
  279.   
  280.   
  281.     private void clearSharePassword() {  
  282.         SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);  
  283.         share.edit().putString(SHARE_LOGIN_PASSWORD, "").commit();  
  284.         share = null;  
  285.     }  
  286.   
  287.     class LoginFailureHandler implements Runnable {  
  288.   
  289.         public void run() {  
  290.             userName = view_userName.getText().toString();  
  291.             password = view_password.getText().toString();  
  292.               
  293.             String validateURL="http://10.0.2.2:8080/androidShopServer/loginCheck.action?userName="  
  294.                 + userName + "&password=" + password;  
  295.             boolean loginState = validateLocalLogin(userName, password,  
  296.                     validateURL);  
  297.             Log.d(this.toString(), "validateLogin");  
  298.   
  299.             if(loginState)   
  300.             {     
  301.                 Intent intent = new Intent();  
  302.                 intent.setClass(Login.this, IndexPage.class);  
  303.                 Bundle bundle = new Bundle();  
  304.                 bundle.putString("MAP_USERNAME", userName);  
  305.                 intent.putExtras(bundle);  
  306.                   
  307.                 startActivity(intent);  
  308.                 proDialog.dismiss();  
  309.             } else {  
  310.                   
  311.                 Message message = new Message();  
  312.                 Bundle bundle = new Bundle();  
  313.                 bundle.putBoolean("isNetError", isNetError);  
  314.                 message.setData(bundle);  
  315.                 loginHandler.sendMessage(message);  
  316.             }  
  317.         }  
  318.   
  319.     }