pmtpenza

Untitled

Oct 31st, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. package com.example.mllsh.oborudovanie2;
  2.  
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.text.TextUtils;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.ProgressBar;
  11. import android.widget.Toast;
  12.  
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15.  
  16. import java.util.HashMap;
  17.  
  18. public class activity_autoriz extends AppCompatActivity {
  19.  
  20. EditText editLogin, editPassword;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_autoriz);
  26.  
  27. editLogin = (EditText) findViewById(R.id.editlogin);
  28. editPassword = (EditText) findViewById(R.id.editPassword);
  29. findViewById(R.id.singin).setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View view) {
  32. userLogin();
  33. }
  34. });
  35.  
  36. //if user presses on not registered
  37. findViewById(R.id.textViewRegister).setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View view) {
  40. //open register screen
  41. finish();
  42. startActivity(new Intent(getApplicationContext(), MainActivity.class));
  43. }
  44. });
  45. }
  46.  
  47. private void userLogin() {
  48. //first getting the values
  49. final String login = editLogin.getText().toString();
  50. final String password = editPassword.getText().toString();
  51.  
  52. //validating inputs
  53. if (TextUtils.isEmpty(login)) {
  54. editLogin.setError("Please enter your username");
  55. editLogin.requestFocus();
  56. return;
  57. }
  58.  
  59. if (TextUtils.isEmpty(password)) {
  60. editPassword.setError("Please enter your password");
  61. editPassword.requestFocus();
  62. return;
  63. }
  64.  
  65. //if everything is fine
  66.  
  67. class userLogin extends AsyncTask <Void, Void, String> {
  68.  
  69. ProgressBar progressBar;
  70.  
  71. @Override
  72. protected void onPreExecute() {
  73. super.onPreExecute();
  74. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  75. progressBar.setVisibility(View.VISIBLE);
  76. }
  77.  
  78. @Override
  79. protected void onPostExecute(String s) {
  80. super.onPostExecute(s);
  81. progressBar.setVisibility(View.GONE);
  82.  
  83.  
  84. try {
  85. //converting response to json object
  86. JSONObject obj = new JSONObject(s);
  87.  
  88. //if no error in response
  89. if (!obj.getBoolean("error")) {
  90. Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
  91.  
  92. //getting the user from the response
  93. JSONObject userJson = obj.getJSONObject("user");
  94.  
  95. //creating a new user object
  96. User user = new User(
  97. userJson.getInt("id"),
  98. userJson.getString("login"),
  99. userJson.getString("username"),
  100. userJson.getString("fio"),
  101. userJson.getString("otchestvo"),
  102. userJson.getString("rol")
  103. );
  104.  
  105. //storing the user in shared preferences
  106. SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);
  107.  
  108. //starting the profile activity
  109. finish();
  110. if(user.getRol().equals("admin") || user.getRol().equals("snab")){
  111. startActivity(new Intent(getApplicationContext(), AdminActivity.class));
  112. }else{
  113. startActivity(new Intent(getApplicationContext(), Snab.class));
  114. }} else {
  115. Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
  116. }
  117. } catch (JSONException e) {
  118. e.printStackTrace();
  119. }
  120. }
  121.  
  122. @Override
  123. protected String doInBackground(Void... voids) {
  124. //creating request handler object
  125. RequestHandler requestHandler = new RequestHandler();
  126.  
  127. //creating request parameters
  128. HashMap<String, String> params = new HashMap<>();
  129. params.put("login", login);
  130. params.put("password", password);
  131.  
  132. //returing the response
  133. return requestHandler.sendPostRequest(URLs.URL_LOGIN, params);
  134. }
  135. }
  136.  
  137. userLogin ul = new userLogin();
  138. ul.execute();
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment