Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends Activity implements OnClickListener {
- class AttemptLogin extends AsyncTask<String, String, String> {
- boolean failure = false;
- String success;
- AlertDialog.Builder builder = new AlertDialog.Builder(
- LoginActivity.this);
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(LoginActivity.this);
- pDialog.setMessage("Logging in. Please wait...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(false);
- pDialog.show();
- }
- @Override
- protected String doInBackground(String... args) {
- String userName = edtUname.getText().toString();
- String docID = edtPass.getText().toString();
- try {
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("usrName", userName));
- params.add(new BasicNameValuePair("dcID", docID));
- Log.d("request!", "starting");
- LOGIN_URL = LOGIN_URL + " &username=" + userName
- + " &password=" + docID;
- JSONObject json = jsonParser.getJSONFromUrl(LOGIN_URL);
- success = json.optString("auth");
- if (success.equals("Yes")) {
- Log.d("Successfully Login!", json.toString());
- return json.optString(TAG_MESSAGE);
- } else {
- return json.optString(TAG_MESSAGE);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- protected void onPostExecute(String message) {
- pDialog.dismiss();
- if (success.equals("Yes")) {
- builder.setMessage("do you want to save your credentials ?")
- .setCancelable(false)
- .setPositiveButton("Yes",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int id) {
- String userName = edtUname.getText()
- .toString();
- String docID = edtPass.getText()
- .toString();
- sharedpreferences = getSharedPreferences(
- MyPREFERENCES,
- Context.MODE_PRIVATE);
- Editor editor = sharedpreferences
- .edit();
- editor.putString("usrIdkey", userName);
- editor.putString("usrPasskey", docID);
- editor.commit();
- Intent ii = new Intent(
- LoginActivity.this,
- PatientDetailsActivity.class);
- startActivity(ii);
- Toast.makeText(getApplicationContext(),
- "Login Successfully...",
- Toast.LENGTH_SHORT).show();
- finish();
- }
- })
- .setNegativeButton("No",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int id) {
- SharedPreferences sharedpreferences = getSharedPreferences(
- "MyPrefs", Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedpreferences
- .edit();
- editor.clear();
- editor.commit();
- Intent ii = new Intent(
- LoginActivity.this,
- PatientDetailsActivity.class);
- startActivity(ii);
- finish();
- // Action for 'NO' Button
- dialog.cancel();
- }
- });
- // Creating dialog box
- AlertDialog alert = builder.create();
- // Setting the title manually
- alert.setTitle("User Credentials");
- // Showing alert dialog box
- alert.show();
- }
- if (success.equals("Yes")) {
- } else {
- Toast.makeText(LoginActivity.this, "Login Failed...",
- Toast.LENGTH_SHORT).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement