Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. public class LoginActivity extends AppCompatActivity {
  2.  
  3. private EditText login = null;
  4. private EditText password = null;
  5. private RadioButton radioButton = null;
  6. private Button button = null;
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_login);
  12.  
  13. login = (EditText)findViewById(R.id.loginEditText);
  14. password = (EditText)findViewById(R.id.passwordEditText);
  15. radioButton = (RadioButton)findViewById(R.id.saveRadioButton);
  16. button = (Button)findViewById(R.id.ConnectionButton);
  17.  
  18. button.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21.  
  22. if (login.getText().toString().equals("")
  23. || password.getText().toString().equals("")) {
  24. alert(getString(R.string.alertDialoguErrorTitle), getString(R.string.UnfilledFieldLogin));
  25. } else {
  26. boolean haveToSave = radioButton.isChecked();
  27.  
  28. User user = User.getUser(login.getText().toString(), password.getText().toString());
  29. try {
  30. Intranet.login.start();
  31. Intranet.login.join();
  32. Intranet.login.interrupt();
  33. Intranet.login.join();
  34. } catch (InterruptedException e) {
  35. alert(getString(R.string.alertDialoguErrorTitle), e.toString());
  36. login.setText("");
  37. password.setText("");
  38. } finally {
  39. if (!user._token.equals("")) {
  40. if (haveToSave) {
  41. // SAVE DATA
  42. }
  43. finish();
  44. } else {
  45. login.setText("");
  46. password.setText("");
  47. alert(getString(R.string.alertDialoguErrorTitle), getString(R.string.badLoginPassword));
  48. }
  49. }
  50. }
  51. }
  52. });
  53. }
  54.  
  55. public void alert(String title, String message) {
  56. AlertDialog.Builder alertDialog = new AlertDialog.Builder(LoginActivity.this);
  57. alertDialog.setTitle(title);
  58. alertDialog.setMessage(message);
  59. alertDialog.setPositiveButton("Close",
  60. new DialogInterface.OnClickListener() {
  61. public void onClick(DialogInterface dialog, int which) {
  62. // nothing
  63. }
  64. });
  65. alertDialog.show();
  66. }
  67.  
  68. }
  69.  
  70. public class Intranet {
  71.  
  72. public static int responseCode = 0;
  73. public static String responseString = "";
  74.  
  75. public static Thread login = new Thread(new Runnable() {
  76. private OkHttpClient client = new OkHttpClient();
  77. private String url = "https://epitech-api.herokuapp.com/login";
  78. private User user = User.getUser();
  79.  
  80. public void run() {
  81. try {
  82. // Build the request
  83. RequestBody formBody = new FormEncodingBuilder()
  84. .add("login", user._login)
  85. .add("password", user._password)
  86. .build();
  87. Request request = new Request.Builder()
  88. .url(url)
  89. .post(formBody)
  90. .build();
  91. Response responses = null;
  92.  
  93. // Reset the response code
  94. responseCode = 0;
  95.  
  96. // Make the request
  97. responses = client.newCall(request).execute();
  98.  
  99. if ((responseCode = responses.code()) == 200) {
  100. // Get response
  101. String jsonData = responses.body().string();
  102.  
  103. // Transform reponse to JSon Object
  104. JSONObject json = new JSONObject(jsonData);
  105.  
  106. // Use the JSon Object
  107. user._token = json.getString("token");
  108. }
  109.  
  110. } catch (IOException e) {
  111. responseString = e.toString();
  112. } catch (JSONException e) {
  113. responseString = e.toString();
  114. }
  115. ;
  116. }
  117. });
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement