Advertisement
Guest User

Untitled

a guest
Mar 12th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. public class LoginActivity extends Activity {
  2. Button btnLogin;
  3. Button btnLinkToRegister;
  4. EditText inputNric;
  5. EditText inputPassword;
  6. TextView loginErrorMsg;
  7.  
  8.  
  9. // Progress Dialog
  10. private ProgressDialog pDialog;
  11.  
  12. // JSON Response node names
  13. //success is the column name of the database - KEY_SUCCESS is we create the name
  14. private static String KEY_SUCCESS = "success";
  15. private static String KEY_ERROR = "error";
  16. private static String KEY_ERROR_MSG = "error_msg";
  17. private static String KEY_NAME = "name";
  18. private static String KEY_NRIC = "nric";
  19. private static String KEY_CREATED_AT = "created_at";
  20.  
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.login);
  25.  
  26. //StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  27. // StrictMode.setThreadPolicy(policy);
  28.  
  29. // Importing all assets like buttons, text fields
  30. inputNric = (EditText) findViewById(R.id.loginNric);
  31. inputPassword = (EditText) findViewById(R.id.loginPassword);
  32. btnLogin = (Button) findViewById(R.id.buttonLogin);
  33. btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
  34. loginErrorMsg = (TextView) findViewById(R.id.login_error);
  35.  
  36. // login button click event
  37. btnLogin.setOnClickListener(new View.OnClickListener() {
  38.  
  39. public void onClick(View arg0) {
  40. // starting background task to update case
  41. new LoginUser().execute();
  42. }
  43. });
  44.  
  45. /*// Link to Register Screen
  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. * Background Async Task to Login User
  59. * */
  60. class LoginUser extends AsyncTask<String, String, String> {
  61.  
  62. /**
  63. * Before starting background thread Show Progress Dialog
  64. * */
  65. protected void onPreExecute() {
  66. super.onPreExecute();
  67. pDialog = new ProgressDialog(LoginActivity.this);
  68. pDialog.setMessage("Logging In ...");
  69. pDialog.setIndeterminate(false);
  70. pDialog.setCancelable(true);
  71. pDialog.show();
  72. }
  73.  
  74. /**
  75. * Logging in
  76. * */
  77. protected String doInBackground(String... params) {
  78. // updating UI from Background Thread
  79. runOnUiThread(new Runnable() {
  80. public void run() {
  81. String nric = inputNric.getText().toString();
  82. String password = inputPassword.getText().toString();
  83. UserFunctions userFunction = new UserFunctions();
  84. Log.d("Button", "Login");
  85. JSONObject json = userFunction.loginUser(nric, password);
  86.  
  87. // check for login response
  88. try {
  89. if (json.getString(KEY_SUCCESS) != null) {
  90. loginErrorMsg.setText("");
  91. String res = json.getString(KEY_SUCCESS);
  92. if(Integer.parseInt(res) == 1){
  93. // user successfully logged in
  94. // Store user details in SQLite Database
  95. DatabaseHandler db = new DatabaseHandler(getApplicationContext());
  96. JSONObject json_user = json.getJSONObject("user");
  97.  
  98. // Clear all previous data in database
  99. userFunction.logoutUser(getApplicationContext());
  100. db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_NRIC), json_user.getString(KEY_CREATED_AT));
  101.  
  102. // Launch home Screen
  103. Intent home = new Intent(getApplicationContext(), AllTabActivity.class);
  104.  
  105. // Close all views before launching home
  106. home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  107. startActivity(home);
  108.  
  109. // Close Login Screen
  110. finish();
  111. }else{
  112. // Error in login
  113. loginErrorMsg.setText("Incorrect username/password");
  114. }
  115. }
  116. } catch (JSONException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. });
  121. return null;
  122. }
  123.  
  124. /**
  125. * After completing background task Dismiss the progress dialog
  126. * **/
  127. protected void onPostExecute(String file_url) {
  128. // dismiss the dialog once case deleted
  129. pDialog.dismiss();
  130.  
  131. }
  132. }
  133.  
  134. }
  135.  
  136. 08-14 17:46:37.766: E/AndroidRuntime(1208): android.os.NetworkOnMainThreadException
  137. 08-14 17:46:37.766: E/AndroidRuntime(1208): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
  138. 08-14 17:46:37.766: E/AndroidRuntime(1208): at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
  139. 08-14 17:46:37.766: E/AndroidRuntime(1208): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
  140. 08-14 17:46:37.766: E/AndroidRuntime(1208): at java.net.InetAddress.getAllByName(InetAddress.java:249)
  141. 08-14 17:46:37.766: E/AndroidRuntime(1208): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
  142. 08-14 17:46:37.766: E/AndroidRuntime(1208): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  143. 08-14 17:46:37.766: E/AndroidRuntime(1208): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
  144. 08-14 17:46:37.766: E/AndroidRuntime(1208): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
  145. 08-14 17:46:37.766: E/AndroidRuntime(1208): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
  146. 08-14 17:46:37.766: E/AndroidRuntime(1208): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
  147. 08-14 17:46:37.766: E/AndroidRuntime(1208): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
  148. 08-14 17:46:37.766: E/AndroidRuntime(1208): at com.pivestigator.JSONParser.makeHttpRequest(JSONParser.java:51)
  149. 08-14 17:46:37.766: E/AndroidRuntime(1208): at com.pivestigator.login.library.UserFunctions.loginUser(UserFunctions.java:40)
  150. 08-14 17:46:37.766: E/AndroidRuntime(1208): at com.pivestigator.login.LoginActivity$LoginUser$1.run(LoginActivity.java:112)
  151. 08-14 17:46:37.766: E/AndroidRuntime(1208): at android.os.Handler.handleCallback(Handler.java:587)
  152. 08-14 17:46:37.766: E/AndroidRuntime(1208): at android.os.Handler.dispatchMessage(Handler.java:92)
  153. 08-14 17:46:37.766: E/AndroidRuntime(1208): at android.os.Looper.loop(Looper.java:132)
  154. 08-14 17:46:37.766: E/AndroidRuntime(1208): at android.app.ActivityThread.main(ActivityThread.java:4025)
  155. 08-14 17:46:37.766: E/AndroidRuntime(1208): at java.lang.reflect.Method.invokeNative(Native Method)
  156. 08-14 17:46:37.766: E/AndroidRuntime(1208): at java.lang.reflect.Method.invoke(Method.java:491)
  157. 08-14 17:46:37.766: E/AndroidRuntime(1208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  158. 08-14 17:46:37.766: E/AndroidRuntime(1208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  159. 08-14 17:46:37.766: E/AndroidRuntime(1208): at dalvik.system.NativeStart.main(Native Method)
  160.  
  161. /**
  162. * Background Async Task to Login User
  163. * */
  164. class LoginUser extends AsyncTask<String, String, String> {
  165.  
  166. /**
  167. * Before starting background thread Show Progress Dialog
  168. * */
  169. protected void onPreExecute() {
  170. super.onPreExecute();
  171. pDialog = new ProgressDialog(LoginActivity.this);
  172. pDialog.setMessage("Logging In ...");
  173. pDialog.setIndeterminate(false);
  174. pDialog.setCancelable(true);
  175. pDialog.show();
  176. }
  177.  
  178. /**
  179. * Logging in
  180. * */
  181. protected String doInBackground(String... params) {
  182. // updating UI from Background Thread
  183. String nric = inputNric.getText().toString();
  184. String password = inputPassword.getText().toString();
  185. UserFunctions userFunction = new UserFunctions();
  186. Log.d("Button", "Login");
  187. JSONObject json = userFunction.loginUser(nric, password);
  188.  
  189. // check for login response
  190. try {
  191. if (json.getString(KEY_SUCCESS) != null) {
  192. loginErrorMsg.setText("");
  193. String res = json.getString(KEY_SUCCESS);
  194. if(Integer.parseInt(res) == 1){
  195. // user successfully logged in
  196. // Store user details in SQLite Database
  197. DatabaseHandler db = new DatabaseHandler(getApplicationContext());
  198. JSONObject json_user = json.getJSONObject("user");
  199.  
  200. // Clear all previous data in database
  201. userFunction.logoutUser(getApplicationContext());
  202. db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_NRIC), json_user.getString(KEY_CREATED_AT));
  203.  
  204. runOnUiThread(new Runnable() {
  205. public void run() {
  206.  
  207. // Launch home Screen
  208. Intent home = new Intent(getApplicationContext(), AllTabActivity.class);
  209.  
  210. // Close all views before launching home
  211. home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  212. startActivity(home);
  213.  
  214. // Close Login Screen
  215. finish();
  216. }else{
  217. // Error in login
  218. loginErrorMsg.setText("Incorrect username/password");
  219. }
  220. }
  221. });
  222. }
  223. } catch (JSONException e) {
  224. e.printStackTrace();
  225. }
  226.  
  227. return null;
  228. }
  229.  
  230. /**
  231. * After completing background task Dismiss the progress dialog
  232. * **/
  233. protected void onPostExecute(String file_url) {
  234. // dismiss the dialog once case deleted
  235. pDialog.dismiss();
  236.  
  237. }
  238. }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement