Login services

import java.util.HashMap;
04import java.util.Map;
05
06import org.json.JSONObject;
07
08import android.app.Activity;
09import android.content.Intent;
10import android.os.Bundle;
11import android.view.View;
12import android.view.View.OnClickListener;
13import android.widget.Button;
14import android.widget.EditText;
15
16import com.example.mifi.utils.DialogUtil;
17import com.example.mifi.utils.HttpUtil;
18
19public 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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值