简单的登录逻辑代码
[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 | package com.example.mifi; import java.util.HashMap; import java.util.Map; import org.json.JSONObject; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import com.example.mifi.utils.DialogUtil; import com.example.mifi.utils.HttpUtil; public class Login extends Activity { EditText etName, etPass; Button bnLogin, bnCancel; public void 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( new OnClickListener() { public void onClick(View v) { if (!validate()) return ; if (loginPro()) { Intent intent = new Intent(Login. this , Main. class ); startActivity(intent); finish(); return ; } DialogUtil.showDialog(Login. this , "用户名或密码错误,请重新输入!" , false ); } }); } /** * 验证登录帐号 */ private boolean loginPro() { String username = etName.getText().toString().trim(); String password = etPass.getText().toString().trim(); JSONObject jsonObj; try { jsonObj = Query(username, password); if (jsonObj.getBoolean( "success" )) return true ; } catch (Exception e) { DialogUtil.showDialog( this , "服务器响应异常,请稍后再试!" , false ); e.printStackTrace(); } return false ; } /** * 验证用户名密码 */ private JSONObject Query(String username, String password) throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put( "username" , username); map.put( "password" , password); String url = HttpUtil.BASE_URL + "client/login/" ; return new JSONObject(HttpUtil.doPost(url, map)); } /** * 输入为空 */ private boolean validate() { String username = etName.getText().toString().trim(); String password = etPass.getText().toString().trim(); if ( "" .equals(username)) { DialogUtil.showDialog( this , "请输入用户名!" , false ); return false ; } if ( "" .equals(password)) { DialogUtil.showDialog( this , "请输入用户密码!" , false ); return false ; } return true ; } } |