Advertisement
Guest User

Untitled

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