Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. package com.ryudhis.aryayudhistira;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.net.ConnectivityManager;
  8.  
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15.  
  16. import androidx.appcompat.app.AppCompatActivity;
  17.  
  18. import com.android.volley.Request;
  19. import com.android.volley.Response;
  20. import com.android.volley.VolleyError;
  21. import com.android.volley.toolbox.StringRequest;
  22. import com.ryudhis.login.AppController;
  23. import com.ryudhis.login.Server;
  24.  
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27.  
  28. import java.util.HashMap;
  29. import java.util.Map;
  30.  
  31. /**
  32. * Created by Kuncoro on 03/24/2017.
  33. */
  34. public class Login extends AppCompatActivity {
  35.  
  36. ProgressDialog pDialog;
  37. Button btn_register, btn_login;
  38. EditText txt_username, txt_password;
  39. Intent intent;
  40.  
  41. int success;
  42. ConnectivityManager conMgr;
  43.  
  44. private String url = Server.URL + "login.php";
  45.  
  46. private static final String TAG = Login.class.getSimpleName();
  47.  
  48. private static final String TAG_SUCCESS = "success";
  49. private static final String TAG_MESSAGE = "message";
  50.  
  51. public final static String TAG_USERNAME = "username";
  52. public final static String TAG_ID = "id";
  53.  
  54. String tag_json_obj = "json_obj_req";
  55.  
  56. SharedPreferences sharedpreferences;
  57. Boolean session = false;
  58. String id, username;
  59. public static final String my_shared_preferences = "my_shared_preferences";
  60. public static final String session_status = "session_status";
  61.  
  62. @Override
  63. protected void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. setContentView(R.layout.activity_login);
  66.  
  67. conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  68. {
  69. if (conMgr.getActiveNetworkInfo() != null
  70. && conMgr.getActiveNetworkInfo().isAvailable()
  71. && conMgr.getActiveNetworkInfo().isConnected()) {
  72. } else {
  73. Toast.makeText(getApplicationContext(), "No Internet Connection",
  74. Toast.LENGTH_LONG).show();
  75. }
  76. }
  77.  
  78. btn_login = (Button) findViewById(R.id.btn_login);
  79. btn_register = (Button) findViewById(R.id.btn_register);
  80. txt_username = (EditText) findViewById(R.id.txt_username);
  81. txt_password = (EditText) findViewById(R.id.txt_password);
  82.  
  83. // Cek session login jika TRUE maka langsung buka MainActivity
  84. sharedpreferences = getSharedPreferences(my_shared_preferences, Context.MODE_PRIVATE);
  85. session = sharedpreferences.getBoolean(session_status, false);
  86. id = sharedpreferences.getString(TAG_ID, null);
  87. username = sharedpreferences.getString(TAG_USERNAME, null);
  88.  
  89. if (session) {
  90. Intent intent = new Intent(Login.this, MainActivity.class);
  91. intent.putExtra(TAG_ID, id);
  92. intent.putExtra(TAG_USERNAME, username);
  93. finish();
  94. startActivity(intent);
  95. }
  96.  
  97.  
  98. btn_login.setOnClickListener(new View.OnClickListener() {
  99.  
  100. @Override
  101. public void onClick(View v) {
  102. // TODO Auto-generated method stub
  103. String username = txt_username.getText().toString();
  104. String password = txt_password.getText().toString();
  105.  
  106. // mengecek kolom yang kosong
  107. if (username.trim().length() > 0 && password.trim().length() > 0) {
  108. if (conMgr.getActiveNetworkInfo() != null
  109. && conMgr.getActiveNetworkInfo().isAvailable()
  110. && conMgr.getActiveNetworkInfo().isConnected()) {
  111. checkLogin(username, password);
  112. } else {
  113. Toast.makeText(getApplicationContext() ,"No Internet Connection", Toast.LENGTH_LONG).show();
  114. }
  115. } else {
  116. // Prompt user to enter credentials
  117. Toast.makeText(getApplicationContext() ,"Kolom tidak boleh kosong", Toast.LENGTH_LONG).show();
  118. }
  119. }
  120. });
  121.  
  122. btn_register.setOnClickListener(new View.OnClickListener() {
  123.  
  124. @Override
  125. public void onClick(View v) {
  126. // TODO Auto-generated method stub
  127. intent = new Intent(Login.this, register.class);
  128. finish();
  129. startActivity(intent);
  130. }
  131. });
  132.  
  133. }
  134.  
  135. private void checkLogin(final String username, final String password) {
  136. pDialog = new ProgressDialog(this);
  137. pDialog.setCancelable(false);
  138. pDialog.setMessage("Logging in ...");
  139. showDialog();
  140.  
  141. StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
  142.  
  143. @Override
  144. public void onResponse(String response) {
  145. Log.e(TAG, "Login Response: " + response.toString());
  146. hideDialog();
  147.  
  148. try {
  149. JSONObject jObj = new JSONObject(response);
  150. success = jObj.getInt(TAG_SUCCESS);
  151.  
  152. // Check for error node in json
  153. if (success == 1) {
  154. String username = jObj.getString(TAG_USERNAME);
  155. String id = jObj.getString(TAG_ID);
  156.  
  157. Log.e("Successfully Login!", jObj.toString());
  158.  
  159. Toast.makeText(getApplicationContext(), jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
  160.  
  161. // menyimpan login ke session
  162. SharedPreferences.Editor editor = sharedpreferences.edit();
  163. editor.putBoolean(session_status, true);
  164. editor.putString(TAG_ID, id);
  165. editor.putString(TAG_USERNAME, username);
  166. editor.commit();
  167.  
  168. // Memanggil main activity
  169. Intent intent = new Intent(Login.this, MainActivity.class);
  170. intent.putExtra(TAG_ID, id);
  171. intent.putExtra(TAG_USERNAME, username);
  172. finish();
  173. startActivity(intent);
  174. } else {
  175. Toast.makeText(getApplicationContext(),
  176. jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
  177.  
  178. }
  179. } catch (JSONException e) {
  180. // JSON error
  181. e.printStackTrace();
  182. }
  183.  
  184. }
  185. }, new Response.ErrorListener() {
  186.  
  187. @Override
  188. public void onErrorResponse(VolleyError error) {
  189. Log.e(TAG, "Login Error: " + error.getMessage());
  190. Toast.makeText(getApplicationContext(),
  191. error.getMessage(), Toast.LENGTH_LONG).show();
  192.  
  193. hideDialog();
  194.  
  195. }
  196. }) {
  197.  
  198. @Override
  199. protected Map<String, String> getParams() {
  200. // Posting parameters to login url
  201. Map<String, String> params = new HashMap<String, String>();
  202. params.put("username", username);
  203. params.put("password", password);
  204.  
  205. return params;
  206. }
  207.  
  208. };
  209.  
  210. // Adding request to request queue
  211. AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
  212. }
  213.  
  214. private void showDialog() {
  215. if (!pDialog.isShowing())
  216. pDialog.show();
  217. }
  218.  
  219. private void hideDialog() {
  220. if (pDialog.isShowing())
  221. pDialog.dismiss();
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement