Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 开启新线程用于登录
- * 并对服务器返回数据进行判断
- * 以及对存储登录信息进行判断
- * error == 0 时服务器判定登录成功
- * 则进行对返回数据的存储以便后面调用
- * 如果存储失败则需要重新登录
- * error == 1 时服务器判定登录失败
- * 需要重新登录
- */
- public void Login() {
- mButton.setClickable(false);
- mButton.setText("Loading...");
- final String username = mUserName.getText().toString().trim();
- final String password = mPassWord.getText().toString().trim();
- new Thread() {
- public void run() {
- final String result = NetUtil.loginByGet(username, password);
- if (result != null) {
- try {
- JSONTokener jsonTokener = new JSONTokener(result);
- JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
- final String message = jsonObject.getString("message");
- if (jsonObject.getInt("error") == 0) {
- String token = jsonObject.getString("token");
- boolean isSaveSuccess = InfoUtil.saveUserInfo(LoginActivity.this,token);
- if (isSaveSuccess) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
- initIntent(MainActivity.class);
- finish();
- }
- });
- }else {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(LoginActivity.this, "保存登录信息失败", Toast.LENGTH_SHORT).show();
- mButton.setClickable(true);
- mButton.setText("登录");
- }
- });
- }
- } else if(jsonObject.getInt("error") == 1) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(LoginActivity.this, "" + message,
- Toast.LENGTH_SHORT).show();
- mButton.setClickable(true);
- mButton.setText("登录");
- }
- });
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(LoginActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
- mButton.setClickable(true);
- mButton.setText("登录");
- }
- });
- }
- }
- }.start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement