Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. private class ProcessLogin extends AsyncTask<String, String, JSONObject> {
  2.  
  3. private ProgressDialog dialog;
  4. private ProgressDialog pDialog;
  5. protected Context applicationContext;
  6. String email,password;
  7.  
  8. @Override
  9. protected void onPreExecute() {
  10. super.onPreExecute();
  11. inputUsername = (EditText) findViewById(R.id.loginUsername);
  12. inputPassword = (EditText) findViewById(R.id.loginPassword);
  13. email = inputUsername.getText().toString();
  14. password = inputPassword.getText().toString();
  15. pDialog = new ProgressDialog(LoginActivity.this);
  16. pDialog.setMessage("Loading User ...");
  17. pDialog.setIndeterminate(false);
  18. pDialog.setCancelable(false);
  19. pDialog.show();
  20. }
  21.  
  22. @Override
  23. protected JSONObject doInBackground(String... args) {
  24.  
  25.  
  26. UserFunctions userFunction = new UserFunctions();
  27. JSONObject json = userFunction.loginUser(email, password);
  28.  
  29. Log.d("Button", "Login");
  30.  
  31.  
  32. return json;
  33.  
  34. }
  35.  
  36. @Override
  37. protected void onPostExecute(JSONObject json) {
  38. // dismiss the dialog after getting all products
  39. pDialog.dismiss();
  40. try {
  41. if (json.getString(KEY_SUCCESS) != null) {
  42. loginErrorMsg.setText("");
  43. String res = json.getString(KEY_SUCCESS);
  44. if(Integer.parseInt(res) == 1){
  45. // user successfully logged in
  46. // Store user details in SQLite Database
  47. DatabaseHandler db = new DatabaseHandler(getApplicationContext());
  48. JSONObject json_user = json.getJSONObject("user");
  49.  
  50. // Clear all previous data in database
  51. userFunction.logoutUser(getApplicationContext());
  52. db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
  53.  
  54. // Launch Dashboard Screen
  55. Intent dashboard = new Intent(getApplicationContext(), DatabaseSample.class);
  56.  
  57. // Close all views before launching Dashboard
  58. dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  59. startActivity(dashboard);
  60.  
  61. // Close Login Screen
  62. finish();
  63. }else{
  64. // Error in login
  65. loginErrorMsg.setText("Incorrect username/password");
  66. }
  67. }
  68. } catch (JSONException e) {
  69. e.printStackTrace();
  70. }
  71.  
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement