Guest User

Untitled

a guest
Jan 19th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. {
  2. "User": [
  3. {
  4. "SSN": "123456789",
  5. "FirstName": "Furkan",
  6. "User_Role": "1"
  7. }
  8. ]
  9. }
  10.  
  11. public class UserList {
  12.  
  13. @SerializedName("User")
  14. private List<User> user;
  15.  
  16. public List<User> getUser() {
  17. return user;
  18. }
  19.  
  20. public void setUser(ArrayList<User> user) {
  21. this.user = user;
  22. }
  23. }
  24.  
  25. public class User {
  26. @SerializedName("SSN")
  27. public String sSN;
  28. @SerializedName("FirstName")
  29. public String firstName;
  30. @SerializedName("User_Role")
  31. public intuserRole;
  32.  
  33. public String getsSN() {
  34. return sSN;
  35. }
  36.  
  37. public void setsSN(String sSN) {
  38. this.sSN = sSN;
  39. }
  40.  
  41. public String getFirstName() {
  42. return firstName;
  43. }
  44.  
  45. public void setFirstName(String firstName) {
  46. this.firstName = firstName;
  47. }
  48. public int getUserRoleTypeId() {
  49. return userRoleTypeId;
  50. }
  51.  
  52. public void setUserRoleTypeId(int userRoleTypeId) {
  53. this.userRoleTypeId = userRoleTypeId;
  54. }
  55. }
  56.  
  57. public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
  58. Button bSignUp, bLogin;
  59. EditText etPassword, etEmail;
  60. private int userRoleTypeId;
  61. private SharedPreferences sharedPreferences;
  62. private ProgressDialog progressDialog;
  63. Intent intent;
  64.  
  65. protected void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.activity_login);
  68.  
  69. bSignUp = (Button) findViewById(R.id.bSignUp);
  70. bLogin = (Button) findViewById(R.id.bLogin);
  71. bSignUp.setOnClickListener((View.OnClickListener) this);
  72. bLogin.setOnClickListener((View.OnClickListener) this);
  73. etPassword = (EditText) findViewById(R.id.etPassword);
  74. etEmail = (EditText) findViewById(R.id.etEmail);
  75. progressDialog = new ProgressDialog(this);
  76. progressDialog.setTitle("Logging in");
  77. progressDialog.setMessage("Please wait ....");
  78.  
  79. sharedPreferences = getApplicationContext().getSharedPreferences("LoginActivity",MODE_PRIVATE);
  80.  
  81. bLogin.setOnClickListener(this);
  82. }
  83.  
  84. @Override
  85. public void onClick(View view) {
  86. switch (view.getId()) {
  87. case R.id.bSignUp:
  88. Intent SignUpIntent = new Intent(this, SignUpActivity.class);
  89. startActivity(SignUpIntent);
  90. break;
  91. case R.id.bLogin:
  92. String email = etEmail.getText().toString();
  93. String password = etPassword.getText().toString();
  94. if (!email.isEmpty() && !password.isEmpty()) {
  95. progressDialog.show();
  96. loginProcess(email, password);
  97. this.userRoleTypeId = sharedPreferences.getInt(Constants.USERROLE, 0);
  98. if (userRoleTypeId == 1) {
  99. intent = new Intent(this, OrganizerHomeActivity.class);
  100. startActivity(intent);
  101. } else if(userRoleTypeId == 2 || userRoleTypeId == 3) {
  102. intent = new Intent(this, UserHomeActivity.class);
  103. startActivity(intent);
  104. }
  105. else{
  106. Toast.makeText(LoginActivity.this, "Wrong email or password", Toast.LENGTH_SHORT).show();
  107. }
  108. } else {
  109. Toast.makeText(LoginActivity.this, "Fields are empty", Toast.LENGTH_SHORT).show();
  110. }
  111.  
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117.  
  118. private void loginProcess(String email, String password) {
  119. LoginAPI loginAPI = RetrofitService.getClient().create(LoginAPI.class);
  120. retrofit2.Call<UserList> call = loginAPI.loginUser(email,password);
  121. call.enqueue(new Callback<UserList>(){
  122. @Override
  123. public void onResponse(Call<UserList> call, Response<UserList> response) {
  124. //I am sure the fault in here. In below i tried to take Json response and fetch it into the Shared Preferences.
  125. //List<User> userLists = response.body().getUser();
  126. if(response.isSuccessful()){
  127. SharedPreferences.Editor editor = sharedPreferences.edit();
  128. editor.putBoolean(Constants.IS_LOGGED_IN, true);
  129. // editor.putString(Constants.NAME, userLists.getFirstName);
  130. //editor.putString(Constants.SSN, userLists.getsSN);
  131. // editor.putInt(Constants.USERROLE, userLists.getuserRoleTypeId);
  132. editor.apply();
  133.  
  134.  
  135. // I am seeing this successfull response toast, so i am getting successfull response but the problem here i dont know how to get this Json response into sharedpref.
  136. Toast.makeText(LoginActivity.this, "successfull response", Toast.LENGTH_SHORT).show();
  137. }
  138. progressDialog.dismiss();
  139. }
  140.  
  141. @Override
  142. public void onFailure(Call<UserList> call, Throwable t) {
  143. Toast.makeText(LoginActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
  144. progressDialog.dismiss();
  145. }
  146. });
  147.  
  148. }
  149. }
Add Comment
Please, Sign In to add comment