Advertisement
Guest User

Untitled

a guest
May 12th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. public class LoginActivity extends Activity implements OnClickListener {
  2.  
  3. class AttemptLogin extends AsyncTask<String, String, String> {
  4.  
  5. boolean failure = false;
  6. String success;
  7. AlertDialog.Builder builder = new AlertDialog.Builder(
  8. LoginActivity.this);
  9.  
  10. @Override
  11. protected void onPreExecute() {
  12. super.onPreExecute();
  13. pDialog = new ProgressDialog(LoginActivity.this);
  14. pDialog.setMessage("Logging in. Please wait...");
  15. pDialog.setIndeterminate(false);
  16. pDialog.setCancelable(false);
  17. pDialog.show();
  18. }
  19.  
  20. @Override
  21. protected String doInBackground(String... args) {
  22.  
  23. String userName = edtUname.getText().toString();
  24. String docID = edtPass.getText().toString();
  25. try {
  26. List<NameValuePair> params = new ArrayList<NameValuePair>();
  27. params.add(new BasicNameValuePair("usrName", userName));
  28. params.add(new BasicNameValuePair("dcID", docID));
  29. Log.d("request!", "starting");
  30.  
  31. LOGIN_URL = LOGIN_URL + " &username=" + userName
  32. + " &password=" + docID;
  33.  
  34. JSONObject json = jsonParser.getJSONFromUrl(LOGIN_URL);
  35. success = json.optString("auth");
  36.  
  37. if (success.equals("Yes")) {
  38.  
  39. Log.d("Successfully Login!", json.toString());
  40.  
  41. return json.optString(TAG_MESSAGE);
  42.  
  43. } else {
  44. return json.optString(TAG_MESSAGE);
  45. }
  46.  
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50.  
  51. return null;
  52. }
  53.  
  54. protected void onPostExecute(String message) {
  55.  
  56. pDialog.dismiss();
  57.  
  58. if (success.equals("Yes")) {
  59. builder.setMessage("do you want to save your credentials ?")
  60. .setCancelable(false)
  61. .setPositiveButton("Yes",
  62. new DialogInterface.OnClickListener() {
  63. public void onClick(DialogInterface dialog,
  64. int id) {
  65. String userName = edtUname.getText()
  66. .toString();
  67. String docID = edtPass.getText()
  68. .toString();
  69.  
  70. sharedpreferences = getSharedPreferences(
  71. MyPREFERENCES,
  72. Context.MODE_PRIVATE);
  73.  
  74. Editor editor = sharedpreferences
  75. .edit();
  76.  
  77. editor.putString("usrIdkey", userName);
  78. editor.putString("usrPasskey", docID);
  79. editor.commit();
  80.  
  81. Intent ii = new Intent(
  82. LoginActivity.this,
  83. PatientDetailsActivity.class);
  84. startActivity(ii);
  85.  
  86. Toast.makeText(getApplicationContext(),
  87. "Login Successfully...",
  88. Toast.LENGTH_SHORT).show();
  89.  
  90. finish();
  91.  
  92. }
  93. })
  94. .setNegativeButton("No",
  95. new DialogInterface.OnClickListener() {
  96. public void onClick(DialogInterface dialog,
  97. int id) {
  98.  
  99. SharedPreferences sharedpreferences = getSharedPreferences(
  100. "MyPrefs", Context.MODE_PRIVATE);
  101. SharedPreferences.Editor editor = sharedpreferences
  102. .edit();
  103. editor.clear();
  104. editor.commit();
  105.  
  106. Intent ii = new Intent(
  107. LoginActivity.this,
  108. PatientDetailsActivity.class);
  109. startActivity(ii);
  110. finish();
  111.  
  112. // Action for 'NO' Button
  113. dialog.cancel();
  114. }
  115. });
  116.  
  117. // Creating dialog box
  118. AlertDialog alert = builder.create();
  119. // Setting the title manually
  120. alert.setTitle("User Credentials");
  121. // Showing alert dialog box
  122. alert.show();
  123. }
  124. if (success.equals("Yes")) {
  125.  
  126. } else {
  127. Toast.makeText(LoginActivity.this, "Login Failed...",
  128. Toast.LENGTH_SHORT).show();
  129. }
  130.  
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement