首页 > 代码库 > Android APP 登陆模块
Android APP 登陆模块
首先我想强调一点,这个登陆的模块最好是放在另外一个线程里面来实现,否则有可能会爆出一系列的问题,
然后再与主UI 交互。这样就不会爆ANR异常
1.对于登陆模块的,首先大体的逻辑肯定是要清晰的。
//在这个登陆另起的线程中
//第一步首先要判断 是否有网络存在
//第二步分别对 是否有网络做相应的处理
//第三步分别对 有网络做相应的处理
//第四步分别对 没有网络做相应的处理
2.确定登陆逻辑之后接下来是如何实现功能的问题,下面我给出最主要的核心代码
a.判断 是否有网络存在
/**
* 判断当前环境下网络是否可用
*
* @param context
* @return true 可用 false 不可用
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager mConnectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mTelephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
// 检查网络连接,如果无网络可用,就不需要进行连网操作等
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if (info == null || !mConnectivity.getBackgroundDataSetting()) {
return false;
}
// 判断网络连接类型,只有在3G或wifi里进行一些数据更新。
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
return info.isConnected();
} else {
return false;
}
}
b.如果是网络可用的话,则继续下一步;如果没有网络的话,就Toast提醒用户开启网络
/**
* 根据用户名和密码登陆
*
* @param uname
* @param pwd
*/
private void login(String uname, String pwd) {
try {
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
HttpClient httpclient = new DefaultHttpClient(httpParams);
String uri = "http://xxx.xxx.xxx.xxxx:8080/api/v1/auth/login";
HttpPost httppost = new HttpPost(uri);
//添加http头信息
httppost.addHeader("Content-Type", "application/json");
//http post的json数据格式: {"id":"TOD4727","passwd":"8111668ebce8f8d27fc2f98391cf20c1"}
JSONObject obj = null;
HttpResponse response = null;
obj = new JSONObject();
obj.put("id", uname);
obj.put("passwd", MD5.GetMD5Code(pwd));
// System.out.println("-----post the data is ++++ " + obj.toString());
httppost.setEntity(new StringEntity(obj.toString()));
response = httpclient.execute(httppost);
int code = response.getStatusLine().getStatusCode();
if (200 == code) { //访问正常
String rev = EntityUtils.toString(response.getEntity());//返回json格式:{"code":"1000","message":"ok","description":""}
System.out.println("-----get the data is ++++ " + rev);
obj = new JSONObject(rev);
String resCode = obj.getString("code");
if("1000".equals(resCode)){//用户名和密码正确
rememberUsernameAndPassword();
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}else if("1002".equals(resCode)){//用户名不存在
Message msg = handler.obtainMessage();
msg.what = USER_NOT_EXIST;
handler.sendMessage(msg);
}else if("1003".equals(resCode)){//密码错误
Message msg = handler.obtainMessage();
msg.what = ERROR_PASSWORD;
handler.sendMessage(msg);
}
}else{//访问出现异常
Message msg = handler.obtainMessage();
msg.what = NET_PROBLEM;
handler.sendMessage(msg);
}
} catch (Exception e) {//访问出现异常
// TODO Auto-generated catch block
Message msg = handler.obtainMessage();
msg.what = NET_PROBLEM;
handler.sendMessage(msg);
e.printStackTrace();
}
}
//这些handler发送的信息交付给handler去处理,做出相应的处理
Android APP 登陆模块