Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. public class Login extends Activity {
  2. private static final String TAG = Register.class.getSimpleName();
  3. private Button btnLogin;
  4. private Button btnLinkToRegister;
  5. private EditText inputEmail;
  6. private EditText inputPassword;
  7. private ProgressDialog pDialog;
  8. private SessionManager session;
  9. private SQLiteHandler db;
  10.  
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_login);
  15.  
  16. inputEmail = (EditText) findViewById(R.id.email);
  17. inputPassword = (EditText) findViewById(R.id.password);
  18. btnLogin = (Button) findViewById(R.id.btnLogin);
  19. btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
  20.  
  21. // Progress dialog
  22. pDialog = new ProgressDialog(this);
  23. pDialog.setCancelable(false);
  24.  
  25. // SQLite database handler
  26. db = new SQLiteHandler(getApplicationContext());
  27.  
  28. // Session manager
  29. session = new SessionManager(getApplicationContext());
  30.  
  31. // Check if user is already logged in or not
  32. if (session.isLoggedIn()) {
  33. // User is already logged in. Take him to main activity
  34. Intent intent = new Intent(Login.this, Payment.class);
  35. startActivity(intent);
  36. finish();
  37. }
  38.  
  39. // Login button Click Event
  40. btnLogin.setOnClickListener(new View.OnClickListener() {
  41.  
  42. public void onClick(View view) {
  43. String email = inputEmail.getText().toString().trim();
  44. String password = inputPassword.getText().toString().trim();
  45.  
  46. // Check for empty data in the form
  47. if (!email.isEmpty() && !password.isEmpty()) {
  48. // login user
  49. checkLogin(email, password);
  50. } else {
  51. // Prompt user to enter credentials
  52. Toast.makeText(getApplicationContext(),
  53. "Please enter the credentials!", Toast.LENGTH_LONG)
  54. .show();
  55. }
  56. }
  57.  
  58. });
  59.  
  60.  
  61.  
  62. // Link to Register Screen
  63. btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
  64.  
  65. public void onClick(View view) {
  66. Intent i = new Intent(getApplicationContext(),
  67. Register.class);
  68. startActivity(i);
  69. finish();
  70. }
  71. });
  72.  
  73. }
  74.  
  75.  
  76. /**
  77. * function to verify login details in mysql db
  78. * */
  79. private void checkLogin(final String email, final String password) {
  80. // Tag used to cancel the request
  81. String tag_string_req = "req_login";
  82.  
  83. pDialog.setMessage("Logging in ...");
  84. showDialog();
  85.  
  86. StringRequest strReq = new StringRequest(Request.Method.POST,
  87. AppConfig.URL_LOGIN, new Response.Listener<String>() {
  88.  
  89. @Override
  90. public void onResponse(String response) {
  91. Log.d(TAG, "Login Response: " + response.toString());
  92. hideDialog();
  93.  
  94. try {
  95. JSONObject jObj = new JSONObject(response);
  96. boolean error = jObj.getBoolean("error");
  97.  
  98. // Check for error node in json
  99. if (!error) {
  100. // user successfully logged in
  101. // Create login session
  102. session.setLogin(true);
  103.  
  104. // Now store the user in SQLite
  105. String uid = jObj.getString("uid");
  106.  
  107. JSONObject user = jObj.getJSONObject("user");
  108. String name = user.getString("name");
  109. String email = user.getString("email");
  110. String created_at = user
  111. .getString("created_at");
  112.  
  113. // Inserting row in users table
  114. db.addUser(name, email, uid, created_at);
  115.  
  116. // Launch main activity
  117. Intent intent = new Intent(Login.this,
  118. Payment.class);
  119. startActivity(intent);
  120. finish();
  121. } else {
  122. // Error in login. Get the error message
  123. String errorMsg = jObj.getString("error_msg");
  124. Toast.makeText(getApplicationContext(),
  125. errorMsg, Toast.LENGTH_LONG).show();
  126. }
  127. } catch (JSONException e) {
  128. // JSON error
  129. e.printStackTrace();
  130. Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  131. }
  132.  
  133. }
  134. }, new Response.ErrorListener() {
  135.  
  136.  
  137. @Override
  138. public void onErrorResponse(VolleyError error) {
  139. Log.e(TAG, "Login Error: " + error.getMessage());
  140. Toast.makeText(getApplicationContext(),
  141. error.getMessage(), Toast.LENGTH_LONG).show();
  142. hideDialog();
  143. }
  144. }) {
  145.  
  146. @Override
  147. protected Map<String, String> getParams() {
  148. // Posting parameters to login url
  149. Map<String, String> params = new HashMap<String, String>();
  150. params.put("email", email);
  151. params.put("password", password);
  152.  
  153. return params;
  154. }
  155.  
  156. };
  157.  
  158. // Adding request to request queue
  159. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  160. }
  161.  
  162. private void showDialog() {
  163. if (!pDialog.isShowing())
  164. pDialog.show();
  165. }
  166.  
  167. private void hideDialog() {
  168. if (pDialog.isShowing())
  169. pDialog.dismiss();
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement