Advertisement
Guest User

loginact

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