import java.util.HashMap; |
04 | import java.util.Map; |
05 |
|
06 | import org.json.JSONObject; |
07 |
|
08 | import android.app.Activity; |
09 | import android.content.Intent; |
10 | import android.os.Bundle; |
11 | import android.view.View; |
12 | import android.view.View.OnClickListener; |
13 | import android.widget.Button; |
14 | import android.widget.EditText; |
15 |
|
16 | import com.example.mifi.utils.DialogUtil; |
17 | import com.example.mifi.utils.HttpUtil; |
18 |
|
19 | public class Login extends Activity { |
20 | EditText etName, etPass; |
21 | Button bnLogin, bnCancel; |
22 |
|
23 | public void onCreate(Bundle savedInstanceState) { |
24 | super .onCreate(savedInstanceState); |
25 | setContentView(R.layout.login); |
26 |
|
27 | etName = (EditText) findViewById(R.id.userEditText); |
28 | etPass = (EditText) findViewById(R.id.pwdEditText); |
29 |
|
30 | bnLogin = (Button) findViewById(R.id.bnLogin); |
31 | bnCancel = (Button) findViewById(R.id.bnCancel); |
32 |
|
33 | bnLogin.setOnClickListener( new OnClickListener() { |
34 | public void onClick(View v) { |
35 | if (!validate()) |
36 | return ; |
37 | if (loginPro()) { |
38 | Intent intent = new Intent(Login. this , Main. class ); |
39 | startActivity(intent); |
40 | finish(); |
41 | return ; |
42 | } |
43 | DialogUtil.showDialog(Login. this , "用户名或密码错误,请重新输入!" , false ); |
44 | } |
45 |
|
46 | }); |
47 | } |
48 |
|
49 | /** |
50 | * 验证登录帐号 |
51 | */ |
52 | private boolean loginPro() { |
53 | String username = etName.getText().toString().trim(); |
54 | String password = etPass.getText().toString().trim(); |
55 | JSONObject jsonObj; |
56 | try { |
57 | jsonObj = Query(username, password); |
58 | if (jsonObj.getBoolean( "success" )) |
59 | return true ; |
60 | } catch (Exception e) { |
61 | DialogUtil.showDialog( this , "服务器响应异常,请稍后再试!" , false ); |
62 | e.printStackTrace(); |
63 | } |
64 | return false ; |
65 | } |
66 |
|
67 | /** |
68 | * 验证用户名密码 |
69 | */ |
70 | private JSONObject Query(String username, String password) throws Exception { |
71 | Map<String, String> map = new HashMap<String, String>(); |
72 | map.put( "username" , username); |
73 | map.put( "password" , password); |
74 | String url = HttpUtil.BASE_URL + "client/login/" ; |
75 | return new JSONObject(HttpUtil.doPost(url, map)); |
76 | } |
77 |
|
78 | /** |
79 | * 输入为空 |
80 | */ |
81 | private boolean validate() { |
82 | String username = etName.getText().toString().trim(); |
83 | String password = etPass.getText().toString().trim(); |
84 | if ( "" .equals(username)) { |
85 | DialogUtil.showDialog( this , "请输入用户名!" , false ); |
86 | return false ; |
87 | } |
88 | if ( "" .equals(password)) { |
89 | DialogUtil.showDialog( this , "请输入用户密码!" , false ); |
90 | return false ; |
91 | } |
92 | return true ; |
93 | } |
94 |
|
95 | } |