Guest User

Untitled

a guest
Sep 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. public void loginPressed(View v){
  2. usernameStr = usernameField.getText().toString();
  3. passwordStr = passwordField.getText().toString();
  4.  
  5. if (!bound) return;
  6. Bundle b = new Bundle();
  7. Message msg = Message.obtain(null, ChatService.LOGIN);
  8. try {
  9. b.putString("username", usernameStr);
  10. b.putString("password", passwordStr);
  11. msg.setData(b);
  12. messenger.send(msg);
  13. }
  14. catch (RemoteException e) {
  15.  
  16. }
  17.  
  18. private class IncomingHandler extends Handler {
  19. @Override
  20. public void handleMessage(Message msg) {
  21. switch(msg.what) {
  22. case LOGIN:
  23.  
  24. Bundle b = msg.getData();
  25. String username = b.getString("username");
  26. String password = b.getString("password");
  27.  
  28. String loginMessage = TCPCall.login(username, password);
  29. connection.sendMessage(loginMessage);
  30.  
  31. String loginReturn = connection.retrieveMessage();
  32. Message m;
  33.  
  34. Scanner s = new Scanner(loginReturn);
  35. s.useDelimiter(",");
  36. String c = s.next();
  37. String status = s.next();
  38. String message = s.next();
  39.  
  40. if (status.equals("OK")) {
  41. m = Message.obtain(null, LoginActivity.OK);
  42. try {
  43. msg.replyTo.send(m);
  44. } catch (RemoteException e) {}
  45. }
  46. else {
  47. m = Message.obtain(null, LoginActivity.ERR);
  48. try {
  49. msg.replyTo.send(m);
  50. } catch (RemoteException e) {}
  51. }
  52. break;
  53.  
  54. msg.replyTo.send(m);
  55.  
  56. static final int LOGIN_STATUS = 1;
  57.  
  58. private class IncomingHandler extends Handler {
  59. @Override
  60. public void handleMessage(Message msg) {
  61. switch(msg.what) {
  62. case LOGIN:
  63.  
  64. Bundle b = msg.getData();
  65. String username = b.getString("username");
  66. String password = b.getString("password");
  67.  
  68. String loginMessage = TCPCall.login(username, password);
  69. connection.sendMessage(loginMessage);
  70.  
  71. String loginReturn = connection.retrieveMessage();
  72. Message m = Message.obtain(null, LOGIN_STATUS);
  73.  
  74. Scanner s = new Scanner(loginReturn);
  75. s.useDelimiter(",");
  76. String c = s.next();
  77. String status = s.next();
  78. String message = s.next();
  79.  
  80. if (status.equals("OK")) {
  81. b.putString("responseC",c);
  82. b.putString("responseStatus",status);
  83. b.putString("responseMessage",message)
  84.  
  85. m.setData(b);
  86. try {
  87. msg.replyTo.send(m);
  88. } catch (RemoteException e) {}
  89. }
  90. else {
  91. /*if something is wrong with username and password you can put
  92. a toast*/
  93.  
  94. }
  95. break;
  96.  
  97. class IncomingHandler extends Handler{
  98.  
  99. @Override
  100. public void handleMessage(Message msg) {
  101. switch (msg.what){
  102. case ChatService.LOGIN_STATUS:
  103. String C = msg.getData().getString("responseC");
  104. String Status = msg.getData().getString("responseStatus");
  105. String Message = msg.getData().getString("responseMessage");
  106.  
  107. //Here is your response in LoginActivity, enjoy!!!
  108.  
  109. break;
  110.  
  111. default:
  112. super.handleMessage(msg);
  113. }
  114. }
  115. }
  116.  
  117. final Messenger mMessenger = new Messenger(new IncomingHandler());
  118.  
  119. public void loginPressed(View v){
  120. usernameStr = usernameField.getText().toString();
  121. passwordStr = passwordField.getText().toString();
  122.  
  123. if (!bound) return;
  124. Bundle b = new Bundle();
  125. Message msg = Message.obtain(null, ChatService.LOGIN_SATUS,0,0);
  126. try {
  127. b.putString("username", usernameStr);
  128. b.putString("password", passwordStr);
  129. msg.setData(b);
  130. msg.replyTo = mMessenger;
  131. messenger.send(msg);
  132. }
  133. catch (RemoteException e) {
  134. // In this case the service has crashed before we could even
  135. // do anything with it; we can count on soon being
  136. // disconnected (and then reconnected if it can be restarted)
  137. // so there is no need to do anything here.
  138.  
  139. }
Add Comment
Please, Sign In to add comment