当我在我的移动应用上测试我的登录时,editor.clear();
无法正常工作,我猜是因为每当我退出并点击登录而我的用户名和密码没有任何值时,它仍然会记录前一个帮我解决我的问题?这是我的代码。
public class loginFragment extends Fragment implements CompoundButton.OnCheckedChangeListener {
final String TAG = this.getClass().getName();
Button btnLogin;
EditText etUsername, etPassword;
TextView tvRegister;
CheckBox cbRemember;
SharedPreferences pref;
SharedPreferences.Editor editor;
boolean checkFlag;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login, container, false);
etUsername = (EditText) v.findViewById(R.id.etFirstname);
etPassword = (EditText) v.findViewById(R.id.etPassword);
btnLogin = (Button) v.findViewById(R.id.btnLogin);
tvRegister = (TextView) v.findViewById(R.id.tvRegister);
cbRemember = (CheckBox) v.findViewById(R.id.cbRemember);
cbRemember.setOnCheckedChangeListener(this);
checkFlag = cbRemember.isChecked();
etUsername.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, etUsername.getText().toString());
}
});
pref = getActivity().getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
editor = pref.edit();
final String username = pref.getString("username", "");
final String password = pref.getString("password", "");
HashMap postData = new HashMap();
postData.put("username", username);
postData.put("password", password);
if (!username.equals("") && (!password.equals(""))) {
PostResponseAsyncTask task1 = new PostResponseAsyncTask(getActivity(), postData,
new AsyncResponse() {
@Override
public void processFinish(String s) {
if (s.contains("renter")) {
Log.d(TAG, s);
Toast.makeText(getActivity(), "Renter Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(getActivity(), RenterTabs.class);
startActivity(in);
getActivity().finish();
} else if (s.contains("owner")) {
Log.d(TAG, s);
Toast.makeText(getActivity(), "Owner Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(getActivity(), OwnerTabs.class);
startActivity(in);
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Wrong username or password...", Toast.LENGTH_SHORT).show();
}
}
});
task1.execute("http://carkila.esy.es/carkila/authenticate.php");
}
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("username", etUsername.getText().toString());
postData.put("password", MD5.encrypt(etPassword.getText().toString()));
PostResponseAsyncTask task1 = new PostResponseAsyncTask(getActivity(), postData,
new AsyncResponse() {
@Override
public void processFinish(String s) {
Log.d(TAG,s);
if (s.contains("renter")) {
if (checkFlag) {
editor.putString("username", etUsername.getText().toString());
editor.putString("password", MD5.encrypt(etPassword.getText().toString()));
editor.apply();
Log.d(TAG, pref.getString("password", ""));
}
Intent in = new Intent(getActivity(), RenterTabs.class);
startActivity(in);
getActivity().finish();
} else if (s.contains("owner")) {
if (checkFlag) {
editor.putString("username", etUsername.getText().toString());
editor.putString("password", MD5.encrypt(etPassword.getText().toString()));
editor.apply();
}
editor.putString("username", etUsername.getText().toString());
editor.putString("password", MD5.encrypt(etPassword.getText().toString()));
editor.apply();
Intent in = new Intent(getActivity(), OwnerTabs.class);
startActivity(in);
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Wrong username or password...", Toast.LENGTH_SHORT).show();
}
}
});
task1.execute("http://carkila.esy.es/carkila/authenticate.php");
}
});
tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getActivity(), RegisterActivity.class);
startActivity(in);
}
});
return v;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkFlag = isChecked;
Log.d(TAG, "checkflag: " + checkFlag);
}
}
public class OwnerTabs extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
SharedPreferences pref;
SharedPreferences.Editor editor;
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_owner_tabs);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(2);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent in = new Intent(OwnerTabs.this, InsertActivity.class);
startActivity(in);
}
});
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position){
case 0:
fab.show();
break;
default:
fab.hide();
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.action_logout){
editor = pref.edit();
editor.clear();
editor.commit();
Intent in = new Intent (getApplicationContext(), MainActivity.class);
startActivity(in);
finish();
}
return super.onOptionsItemSelected(item);
}
<?php
require 'database-config.php';
session_destroy();
$username = "";
$password = "";
if(isset($_POST['username'])){
$username = $_POST['username'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
$q = 'SELECT * FROM tbl_user WHERE username=:username AND password=:password';
$query = $dbh->prepare($q);
$query->execute(array(':username' => $username, ':password' => $password));
if($query->rowCount() == 0){
header('Location: Login.php?err=1');
}else{
$row = $query->fetch(PDO::FETCH_ASSOC);
$_POST['sess_user_id'] = $row['userID'];
$_POST['sess_userrole'] = $row['roles'];
if($_POST['sess_userrole'] == "renter"){
echo "renter";
}else if ($_POST['sess_userrole'] == "owner"){
echo "owner";
}
}
?>
<?php
$database = '****';
$host = '****';
$user = '****';
$pass = '****';
$dbh = new PDO("mysql:dbname={$database};host={$host}", $user, $pass);
try{
if(!$dbh){
echo "unable to connect to database";
}
}catch (PDOException $e){
echo $e->getMessage();
}
?>
答案 0 :(得分:0)
您需要在editor.commit();
方法
editor.clear();