Advertisement
Guest User

Untitled

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