Android在LoginActivity模板中添加验证过程
step1.在工程中添加LoginActivity类
右击工程名,选择new->Activity->Login Activity,如图:
step2.修改doInBackground函数
修改代码如下:
// An internet chek
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("你的URL");
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
revie = EntityUtils.toString(entity,"utf-8");
//GetThread gt = new GetThread();
//gt.start();
}
catch (Exception e) {
return false;
}
finally {
String[] a = revie.split(",");
if (a[0].equals(mEmail) && a[1].equals(mPassword))
return true;
else
return false;
}
}
doInbackground函数在处理后的bool类型返回值将返回给onPostExecute函数
// recvive the bool
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);
//成功则登陆界面结束,跳转至你提前定义好的主界面或者其他界面
if (success) {
finish();
}
//如果接受的success为false,则显示抛出错误
else {
mPasswordView.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
}
}
tips:我是在MainActivity中采用Intent意图进行跳转的方式,所以在finish后将回到MainActivity中
// start Login with Intent
super.onCreate(savedInstanceState);
Intent login_intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(login_intent);