Advertisement
taufiqhidayah97

ini

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