Advertisement
Guest User

Login

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