简单的登录逻辑代码
[1].[代码] [Java]代码 跳至 [1]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | packagecom.example.mifi;importjava.util.HashMap;importjava.util.Map;importorg.json.JSONObject;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.EditText;importcom.example.mifi.utils.DialogUtil;importcom.example.mifi.utils.HttpUtil;publicclass Login extendsActivity { EditText etName, etPass; Button bnLogin, bnCancel; publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); etName = (EditText) findViewById(R.id.userEditText); etPass = (EditText) findViewById(R.id.pwdEditText); bnLogin = (Button) findViewById(R.id.bnLogin); bnCancel = (Button) findViewById(R.id.bnCancel); bnLogin.setOnClickListener(newOnClickListener() { publicvoid onClick(View v) { if(!validate()) return; if(loginPro()) { Intent intent = newIntent(Login.this, Main.class); startActivity(intent); finish(); return; } DialogUtil.showDialog(Login.this,"用户名或密码错误,请重新输入!",false); } }); } /** * 验证登录帐号 */ privateboolean loginPro() { String username = etName.getText().toString().trim(); String password = etPass.getText().toString().trim(); JSONObject jsonObj; try{ jsonObj = Query(username, password); if(jsonObj.getBoolean("success")) returntrue; }catch(Exception e) { DialogUtil.showDialog(this,"服务器响应异常,请稍后再试!",false); e.printStackTrace(); } returnfalse; } /** * 验证用户名密码 */ privateJSONObject Query(String username, String password) throwsException { Map<String, String> map = newHashMap<String, String>(); map.put("username", username); map.put("password", password); String url = HttpUtil.BASE_URL + "client/login/"; returnnew JSONObject(HttpUtil.doPost(url, map)); } /** * 输入为空 */ privateboolean validate() { String username = etName.getText().toString().trim(); String password = etPass.getText().toString().trim(); if("".equals(username)) { DialogUtil.showDialog(this,"请输入用户名!",false); returnfalse; } if("".equals(password)) { DialogUtil.showDialog(this,"请输入用户密码!",false); returnfalse; } returntrue; }} |