Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- inputEmail = (EditText) findViewById(R.id.email);
- inputPassword = (EditText) findViewById(R.id.password);
- btnLogin = (Button) findViewById(R.id.btnLogin);
- btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
- // Progress dialog
- pDialog = new ProgressDialog(this);
- pDialog.setCancelable(false);
- // SQLite database handler
- db = new SQLiteHandler(getApplicationContext());
- // Session manager
- session = new SessionManager(getApplicationContext());
- // Check if user is already logged in or not
- if (session.isLoggedIn()) {
- // User is already logged in. Take him to main activity
- Intent intent = new Intent(LoginActivity.this, Main2Activity.class);
- startActivity(intent);
- finish();
- }
- // Login button Click Event
- btnLogin.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- String email = inputEmail.getText().toString().trim();
- String password = inputPassword.getText().toString().trim();
- // Check for empty data in the form
- if (!email.isEmpty() && !password.isEmpty()) {
- // login user
- checkLogin(email, password);
- } else {
- // Prompt user to enter credentials
- Toast.makeText(getApplicationContext(), "Please enter the credentials!", Toast.LENGTH_LONG).show();
- }
- }
- });
- // Link to Register Screen
- btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
- startActivity(i);
- finish();
- }
- });
- }
- /**
- * function to verify login details in mysql db
- * */
- private void checkLogin(final String email, final String password) {
- // Tag used to cancel the request
- String tag_string_req = "req_login";
- pDialog.setMessage("Logging in ...");
- showDialog();
- StringRequest strReq = new StringRequest(Request.Method.POST,
- AppConfig.URL_LOGIN, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.d(TAG, "Login Response: " + response.toString());
- hideDialog();
- try {
- JSONObject jObj = new JSONObject(response);
- boolean error = jObj.getBoolean("error");
- // Check for error node in json
- if (!error) {
- // user successfully logged in
- // Create login session
- session.setLogin(true);
- // Now store the user in SQLite
- String uid = jObj.getString("uid");
- JSONObject user = jObj.getJSONObject("user");
- String name = user.getString("name");
- String email = user.getString("email");
- String created_at = user.getString("created_at");
- // Inserting row in users table
- db.addUser(name, email, uid, created_at);
- // Launch main activity
- Intent intent = new Intent(LoginActivity.this, Main2Activity.class);
- startActivity(intent);
- finish();
- } else {
- // Error in login. Get the error message
- String errorMsg = jObj.getString("error_msg");
- Toast.makeText(getApplicationContext(),
- errorMsg, Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- // JSON error
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e(TAG, "Login Error: " + error.getMessage());
- Toast.makeText(getApplicationContext(),
- error.getMessage(), Toast.LENGTH_LONG).show();
- hideDialog();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- // Posting parameters to login url
- Map<String, String> params = new HashMap<String, String>();
- params.put("email", email);
- params.put("password", password);
- return params;
- }
- };
- // Adding request to request queue
- AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
- }
- private void showDialog() {
- if (!pDialog.isShowing())
- pDialog.show();
- }
- private void hideDialog() {
- if (pDialog.isShowing())
- pDialog.dismiss();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement