`
lovehui0228
  • 浏览: 100411 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android用户登录

阅读更多

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

 

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

 

Server端,我用的tomcat:

package com.action.userAction;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class LoginCheckAction implements Action{
	
	private String userName;
	private String password;
	
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		userName = request.getParameter("userName");
		password = request.getParameter("password");
	
		
		HttpServletResponse response = ServletActionContext.getResponse();
		
		OutputStream os = response.getOutputStream();  
		
		if(!"lovehui".equals(userName) || !"1989".equals(password))
		{
			os.write(new Integer(-1));
		}
		else
		{
			os.write(new Integer(1));
		}
		
		response.setStatus(HttpServletResponse.SC_OK);
			 
		return null;

	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	

}

 

 

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

package src.siwi.map.android;

import java.io.DataInputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Login extends Activity {
	private String userName;
	private String password;
	
	private EditText view_userName;
	private EditText view_password;
	private CheckBox view_rememberMe;
	private Button view_loginSubmit;
	
	private static final int MENU_EXIT = Menu.FIRST - 1;
	private static final int MENU_ABOUT = Menu.FIRST;
	
	private final String SHARE_LOGIN_TAG = "MAP_SHARE_LOGIN_TAG";

	private String SHARE_LOGIN_USERNAME = "MAP_LOGIN_USERNAME";
	private String SHARE_LOGIN_PASSWORD = "MAP_LOGIN_PASSWORD";
	
	private boolean isNetError;
	
	private ProgressDialog proDialog;

	
	Handler loginHandler = new Handler() {
		public void handleMessage(Message msg) 
		{
			isNetError = msg.getData().getBoolean("isNetError");
			if(proDialog != null) 
			{
				proDialog.dismiss();
			}
			if(isNetError) 
			{
				Toast.makeText(Login.this, "当前网络不可用",
						Toast.LENGTH_SHORT).show();
			}
			else 
			{
				Toast.makeText(Login.this, "错误的用户名或密码",
						Toast.LENGTH_SHORT).show();
				
				clearSharePassword();
			}
		}
	};


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		findViewsById();
		initView(false);
		
		setListener();
	}


	private void findViewsById() {
		view_userName = (EditText)findViewById(R.id.loginUserNameEdit);
		view_password = (EditText)findViewById(R.id.loginPasswordEdit);
		view_rememberMe = (CheckBox)findViewById(R.id.loginRememberMeCheckBox);
		view_loginSubmit = (Button)findViewById(R.id.loginSubmit);
	}

	
	private void initView(boolean isRememberMe) {
		SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
		String userName = share.getString(SHARE_LOGIN_USERNAME, "");
		String password = share.getString(SHARE_LOGIN_PASSWORD, "");
		Log.d(this.toString(), "userName=" + userName + " password=" + password);
		if (!"".equals(userName)) {
			view_userName.setText(userName);
		}
		if (!"".equals(password)) {
			view_password.setText(password);
			view_rememberMe.setChecked(true);
		}
		
		if (view_password.getText().toString().length() > 0) {
			// view_loginSubmit.requestFocus();
			// view_password.requestFocus();
		}
		share = null;
	}

	
	private boolean validateLocalLogin(String userName, String password, String validateUrl) {
		
		boolean loginState = false;
		HttpURLConnection conn = null;
		DataInputStream dis = null;
		try 
		{
			URL url = new URL(validateUrl);
			conn = (HttpURLConnection)url.openConnection();
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");	
			conn.connect();
		
			dis = new DataInputStream(conn.getInputStream());
			if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) 
			{
				Log.d(this.toString(), "HTTP ERROR");
				isNetError = true;
				return false;
			}
			
			int loginStateInt = dis.read();

			Log.v("loginState", String.valueOf(loginStateInt));
			if(loginStateInt == 1) 
			{
				loginState = true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			isNetError = true;
			Log.d(this.toString(), e.getMessage() + "  127 line");
		} 
		finally 
		{
			if(conn != null) 
			{
				conn.disconnect();
			}
		}
		
		if(loginState) 
		{
			if(isRememberMe()) 
			{
				saveSharePreferences(true, true);
			} 
			else 
			{
				saveSharePreferences(true, false);
			}
		} 
		else 
		{
			if(!isNetError)
			{
				clearSharePassword();
			}
		}
		if(!view_rememberMe.isChecked()) 
		{
			clearSharePassword();
		}
		
		Log.v("loginState", String.valueOf(loginState));
		return loginState;
	}

	
	private void saveSharePreferences(boolean saveUserName, boolean savePassword) {
		SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
		if(saveUserName) {
			Log.d(this.toString(), "saveUserName="
					+ view_userName.getText().toString());
			share.edit().putString(SHARE_LOGIN_USERNAME,
					view_userName.getText().toString()).commit();
		}
		if (savePassword) {
			share.edit().putString(SHARE_LOGIN_PASSWORD,
					view_password.getText().toString()).commit();
		}
		share = null;
	}

	
	private boolean isRememberMe() {
		if (view_rememberMe.isChecked()) {
			return true;
		}
		return false;
	}

	
	private OnClickListener submitListener = new OnClickListener() {
	
		public void onClick(View v) {
			proDialog = ProgressDialog.show(Login.this, "请稍候",
					"", true, true);
			
			Thread loginThread = new Thread(new LoginFailureHandler());
			loginThread.start();
		}
	};

	// .start();
	// }
	// };

	
	private OnCheckedChangeListener rememberMeListener = new OnCheckedChangeListener() {


		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (view_rememberMe.isChecked()) {
				Toast.makeText(Login.this, "ischecked",
						Toast.LENGTH_SHORT).show();
			}
		}
	};


	

	
	private void setListener() {
		view_loginSubmit.setOnClickListener(submitListener);
		
		view_rememberMe.setOnCheckedChangeListener(rememberMeListener);
	}

	
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_EXIT, 0, getResources().getText(R.string.MENU_EXIT));
		menu.add(0, MENU_ABOUT, 0, getResources().getText(R.string.MENU_ABOUT));
		return true;
	}


	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		super.onMenuItemSelected(featureId, item);
		switch (item.getItemId()) {
		case MENU_EXIT:
			finish();
			break;
		case MENU_ABOUT:
			alertAbout();
			break;
		}
		return true;
	}

	
	private void alertAbout() {
		new AlertDialog.Builder(Login.this).setTitle(R.string.MENU_ABOUT)
				.setMessage(R.string.aboutInfo).setPositiveButton(
						R.string.ok_label,
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface, int i) {
							}
						}).show();
	}


	private void clearSharePassword() {
		SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
		share.edit().putString(SHARE_LOGIN_PASSWORD, "").commit();
		share = null;
	}

	class LoginFailureHandler implements Runnable {

		public void run() {
			userName = view_userName.getText().toString();
			password = view_password.getText().toString();
			
			String validateURL="http://10.0.2.2:8080/androidShopServer/loginCheck.action?userName="
				+ userName + "&password=" + password;
			boolean loginState = validateLocalLogin(userName, password,
					validateURL);
			Log.d(this.toString(), "validateLogin");

			if(loginState) 
			{	
				Intent intent = new Intent();
				intent.setClass(Login.this, IndexPage.class);
				Bundle bundle = new Bundle();
				bundle.putString("MAP_USERNAME", userName);
				intent.putExtras(bundle);
				
				startActivity(intent);
				proDialog.dismiss();
			} else {
				
				Message message = new Message();
				Bundle bundle = new Bundle();
				bundle.putBoolean("isNetError", isNetError);
				message.setData(bundle);
				loginHandler.sendMessage(message);
			}
		}

	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics