首先我想强调一点。这个登陆的模块最好是放在另外一个线程里面来实现。否则有可能会爆出一系列的问题,
然后再与主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提醒用户开启网络
/**
* 依据username和password登陆
*
* @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)){//username和password正确
rememberUsernameAndPassword();
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}else if("1002".equals(resCode)){//username不存在
Message msg = handler.obtainMessage();
msg.what = USER_NOT_EXIST;
handler.sendMessage(msg);
}else if("1003".equals(resCode)){//password错误
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去处理。做出对应的处理