Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. public void Login() {
  2. mButton.setClickable(false);
  3. mButton.setText("Loading...");
  4. final String username = mUserName.getText().toString().trim();
  5. final String password = mPassWord.getText().toString().trim();
  6.  
  7. new Thread() {
  8. public void run() {
  9. final String result = NetUtil.loginByGet(username, password);
  10. if (result != null) {
  11. try {
  12. JSONTokener jsonTokener = new JSONTokener(result);
  13. JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
  14. final String message = jsonObject.getString("message");
  15. if (jsonObject.getInt("error") == 0) {
  16. String token = jsonObject.getString("token");
  17. boolean isSaveSuccess = InfoUtil.saveUserInfo(LoginActivity.this,token);
  18. if (isSaveSuccess) {
  19. runOnUiThread(new Runnable() {
  20. @Override
  21. public void run() {
  22. Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  23. initIntent(MainActivity.class);
  24. finish();
  25. }
  26. });
  27. }else {
  28. runOnUiThread(new Runnable() {
  29. @Override
  30. public void run() {
  31. Toast.makeText(LoginActivity.this, "保存登录信息失败", Toast.LENGTH_SHORT).show();
  32. mButton.setClickable(true);
  33. mButton.setText("登录");
  34. }
  35. });
  36. }
  37. } else if(jsonObject.getInt("error") == 1) {
  38. runOnUiThread(new Runnable() {
  39. @Override
  40. public void run() {
  41. Toast.makeText(LoginActivity.this, "" + message,
  42. Toast.LENGTH_SHORT).show();
  43. mButton.setClickable(true);
  44. mButton.setText("登录");
  45. }
  46. });
  47. }
  48. }catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. } else {
  52. runOnUiThread(new Runnable() {
  53. @Override
  54. public void run() {
  55. Toast.makeText(LoginActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
  56. mButton.setClickable(true);
  57. mButton.setText("登录");
  58. }
  59. });
  60. }
  61. }
  62. }.start();
  63. }
  64.  
  65. private class LoginTask extends AsyncTask<Void, Void, String>{
  66.  
  67. private String username;
  68. private String password;
  69. private Exception exception;
  70. private boolean isSaveSuccess;
  71.  
  72. public LoginTask(String username, String password){
  73. this.username = username;
  74. this.password = password;
  75. }
  76.  
  77. @Override
  78. protected String doInBackground(Void... voids) {
  79. try{
  80. String result = NetUtil.loginByGet(username, password);
  81. //Grab the token
  82. isSaveSuccess = InfoUtil.saveUserInfo(LoginActivity.this,token);
  83. }catch(Exception ex){
  84. //Catch any given exception to use it in the post execution (e.g. show a toast)
  85. this.exception = ex;
  86. }
  87. }
  88.  
  89. @Override
  90. protected void onPostExecute(String result) {
  91. //Here it goes your UI logic
  92. }
  93.  
  94. }
  95.  
  96. public void doLogin(){
  97. mButton.setClickable(false);
  98. mButton.setText("Loading...");
  99. String username = mUserName.getText().toString().trim();
  100. String password = mPassWord.getText().toString().trim();
  101. new LoginTask(username, password).execute();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement