Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. /**
  2. * 开启新线程用于登录
  3. * 并对服务器返回数据进行判断
  4. * 以及对存储登录信息进行判断
  5. * error == 0 时服务器判定登录成功
  6. * 则进行对返回数据的存储以便后面调用
  7. * 如果存储失败则需要重新登录
  8. * error == 1 时服务器判定登录失败
  9. * 需要重新登录
  10. */
  11. public void Login() {
  12. mButton.setClickable(false);
  13. mButton.setText("Loading...");
  14. final String username = mUserName.getText().toString().trim();
  15. final String password = mPassWord.getText().toString().trim();
  16.  
  17. new Thread() {
  18. public void run() {
  19. final String result = NetUtil.loginByGet(username, password);
  20. if (result != null) {
  21. try {
  22. JSONTokener jsonTokener = new JSONTokener(result);
  23. JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
  24. final String message = jsonObject.getString("message");
  25. if (jsonObject.getInt("error") == 0) {
  26. String token = jsonObject.getString("token");
  27. boolean isSaveSuccess = InfoUtil.saveUserInfo(LoginActivity.this,token);
  28. if (isSaveSuccess) {
  29. runOnUiThread(new Runnable() {
  30. @Override
  31. public void run() {
  32. Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  33. initIntent(MainActivity.class);
  34. finish();
  35. }
  36. });
  37. }else {
  38. runOnUiThread(new Runnable() {
  39. @Override
  40. public void run() {
  41. Toast.makeText(LoginActivity.this, "保存登录信息失败", Toast.LENGTH_SHORT).show();
  42. mButton.setClickable(true);
  43. mButton.setText("登录");
  44. }
  45. });
  46. }
  47. } else if(jsonObject.getInt("error") == 1) {
  48. runOnUiThread(new Runnable() {
  49. @Override
  50. public void run() {
  51. Toast.makeText(LoginActivity.this, "" + message,
  52. Toast.LENGTH_SHORT).show();
  53. mButton.setClickable(true);
  54. mButton.setText("登录");
  55. }
  56. });
  57. }
  58. }catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. } else {
  62. runOnUiThread(new Runnable() {
  63. @Override
  64. public void run() {
  65. Toast.makeText(LoginActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
  66. mButton.setClickable(true);
  67. mButton.setText("登录");
  68. }
  69. });
  70. }
  71. }
  72. }.start();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement