Android用户登录
Android用户登录
前几天和教我们计算机网络的老师讨论了一些关于手机应用发展前景的问题,学到了很多,有技术方面的也有非技术方面的,他的观点就是手机网络游戏的发展大势所趋,即使目前技术还不够成熟。
所以说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);
}
}
}
}
相关文章
- Android手机版移动应用中心操作手册2.0
- 基于Android的计算机基础知识移动学习APP设计
- (完整版)基于安卓的网上订餐系统毕业设计
- 移动办公系统建设方案
- 基于安卓的校园快递
- 移动办公方案
- 挂机短信说明书
- 外文翻译-基于Android智能家居系统
- 芒果移动广告管理平台使用说明
Android 移动应用中心 用户操作手册 中国石化移动应用平台项目组 (2015年12月) 版本控制 变更记录: 审核记录: 目录 1 如何安装ANDROID 版移动应用中心 . ............................. ...
摘 要 本文主要介绍基于Android 平台的计算机基础知识移动学习APP的设计实现过程,该系统客户端基础学习.单元练习.综合测试和错题本功能,可以满足用户基本学习需求,视频及精品教程功能可以丰富用户的学习模式,提升用户的学习效果,服务器端 ...
摘 要 在科学技术高度发展的当今社会,网络信息化已经成为时代的潮 流.随着计算机技术的不断更新变化,特别是Android 操作系统的出现,使移动互联网业务更加蓬勃的发展.根据专家的调查和研究,发现传统的订餐模式已经不能适应市场的高速发展.因 ...
无锡市北塘区政府移动办公系统 建设方案 2014年11月 目 录 一. 项目概述 . ............................................................................. ...
基于Android 的校园快递平台的设计与实现 摘 要:随着智能手机的普及以及移动互联网的快速发展,很多人尤其是在校大学生已经习惯于 使用手机应用来享受生活的便利.本文设计了一款基于Android 的校园快递平台,该平台可以方便快递人员进行 ...
第二章移动办公解决方案 河北省高级人民法院的信息化经过近10年的发展,已经在内部专网部署了多套应用系统用于日常办公.但是当院领导在会议.外出等没有办公环境的时候,常常会面临紧急文件签署的问题.伴随着3G时代来临,这个问题已经迎刃而解.中国联 ...
固网挂机短信业务 使用手册 2014年1月 目录 1 登录 .......................................................................................... ...
通信工程学院 毕业设计外文翻译 毕业设计题目基于ANDRIO的智能家居系统的设计与实现 外文题目UBIQUITOUS SMART HOME SYSTEM USING ANDROID APPLICATION 专业:通信工程 学号: 学生姓名: ...
芒果移动广告智能管理平台使用说明 关于芒果移动广告管理平台............................. 2 如何使用芒果移动广告管理平台......................... 3 一. 注册并完善您的个人资料... ...