Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. public class LoginActivity extends AppCompatActivity {
  2.  
  3. private static final String TAG = LoginActivity.class.getSimpleName();
  4. private Button button;
  5. private EditText editText;
  6. private EditText editText2;
  7. private ProgressDialog pDialog;
  8. private SessionManager session;
  9. private SQLiteHandler db;
  10. CheckBox show_password;
  11. String user_fullname, email, user_id,user_mobile, session_id, module;
  12.  
  13. public LoginActivity(){
  14.  
  15.  
  16. }
  17.  
  18. @Override
  19. public void onCreate(final Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_login);
  22.  
  23. editText = (EditText) findViewById(R.id.editText);
  24. editText2 = (EditText) findViewById(R.id.editText2);
  25. button = (Button) findViewById(R.id.button);
  26. show_password = (CheckBox) findViewById(R.id.show_hide_password);
  27.  
  28. // Progress dialog
  29. pDialog = new ProgressDialog(this);
  30. pDialog.setCancelable(false);
  31.  
  32. // SQLite database handler
  33. db = new SQLiteHandler(getApplicationContext());
  34.  
  35. // Session manager
  36. session = new SessionManager(getApplicationContext());
  37. // get the show/hide password Checkbox
  38.  
  39. // Check if user is already logged in or not
  40. //if (session.isLoggedIn()) {
  41. // User is already logged in. Take him to main activity
  42.  
  43. // Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  44. // startActivity(intent);
  45. // finish();
  46. // }
  47. // Login button Click Event
  48.  
  49. show_password.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  50.  
  51. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  52. // checkbox status is changed from uncheck to checked.
  53. if (!isChecked) {
  54. // show password
  55. editText2.setTransformationMethod(PasswordTransformationMethod.getInstance());
  56. } else {
  57. // hide password
  58. editText2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
  59. }
  60. }
  61. });
  62.  
  63. button.setOnClickListener(new View.OnClickListener() {
  64.  
  65. public void onClick(View view) {
  66. String username = editText.getText().toString().trim();
  67. String password = editText2.getText().toString().trim();
  68.  
  69. // Check for empty data in the form
  70. if (!username.isEmpty() && !password.isEmpty()) {
  71. // login user
  72. checkLogin(username, password);
  73. } else {
  74. // Prompt user to enter credentials
  75. Toast.makeText(getApplicationContext(),
  76. "Please enter the credentials!", Toast.LENGTH_LONG)
  77. .show();
  78. }
  79. }
  80.  
  81. });
  82. }
  83.  
  84. /**
  85. * function to verify login details in mysql db
  86. */
  87. private void checkLogin(final String username, final String password) {
  88.  
  89. pDialog.setMessage("Logging in ...");
  90. showDialog();
  91.  
  92. RequestQueue requestQueue = Volley.newRequestQueue(this);
  93. String URL= "http://crm.sparshnow.com/api/index.php?username="+username+"&password="+password;
  94. final JsonObjectRequest strReq = new JsonObjectRequest(URL,null, new Response.Listener<JSONObject>() {
  95. @Override
  96. public void onResponse(JSONObject response) {
  97. Log.d(TAG, "Login Response: " + response);
  98. hideDialog();
  99.  
  100. try {
  101. String success = response.getString("success");
  102. Log.d("response_value", success);
  103. if (success.equals("TRUE")) {
  104.  
  105. user_fullname=response.getString("user_fullname");
  106. email=response.getString("email");
  107. user_id=response.getString("user_id");
  108. user_mobile=response.getString("user_mobile");
  109. session_id=response.getString("session_id");
  110. Intent intent = new Intent(LoginActivity.this,
  111. MainActivity.class);
  112. intent.putExtra("user_fullname",user_fullname);
  113. intent.putExtra("email",email);
  114. intent.putExtra("user_mobile",user_mobile);
  115. intent.putExtra("session_id", session_id);
  116. session.setLogin(true);
  117. startActivity(intent);
  118. finish();
  119. } else {
  120. Toast.makeText(getApplicationContext(), "Enter Correct Detail ", Toast.LENGTH_LONG).show();
  121. }
  122. } catch (JSONException e) {
  123. e.printStackTrace();
  124. }
  125. }
  126. },
  127.  
  128. new Response.ErrorListener() {
  129. @Override
  130. public void onErrorResponse(VolleyError success) {
  131. Log.e(TAG, "Login Error: " + success.getMessage());
  132. Toast.makeText(getApplicationContext(),
  133. success.getMessage(), Toast.LENGTH_LONG).show();
  134. hideDialog();
  135. }
  136. });
  137. requestQueue.add(strReq);
  138. }
  139. private void showDialog() {
  140. if (!pDialog.isShowing())
  141. pDialog.show();
  142. }
  143.  
  144. private void hideDialog() {
  145. if (pDialog.isShowing())
  146. pDialog.dismiss();
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement