Advertisement
noe21

activity login di android studio

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