我正在使用“共享首选项”来存储用户数据,并在电子商务网站的Web视图应用中设置“登录状态”。用户可以通过登录URL或结帐页面登录。我面临的问题我无法在用户成功登录后将登录状态设置为“True”,因此我可以检索其显示的用户名为" False"。
编辑:在发布问题后,我能够找到登录URL的部分解决方案,将状态设置为true,但CARTURL不起作用。我定义了isRedirected = true;
并设置条件以获得正确的状态。
我在编辑后重新发布代码:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e(TAG, "should override url loading" + url);
view.loadUrl(url);
isRedirected = true;
return true;
}
public void onPageFinished(WebView view, String url) {
//webView.setClickable(true);menuBtn.setClickable(true);cartImg.setClickable(true);logoImg.setClickable(true);
Log.e(TAG, "on page finished" + url);
Log.e(TAG, "is logged in==" + isLoggedin);
isRedirected = true;
String isLoggedin = "false";
if(url.equals(Constants.ACCOUNTURL) && isRedirected && isLoggedin.equals("false")) {
Log.e(TAG, "reload");
//webView.loadUrl(Constants.ACCOUNTEDITURL);
SharedPreferences pref = getApplicationContext().getSharedPreferences("Login", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("isLogedin", "true");
editor.commit();
setWelcome();
Toast toast = Toast.makeText(MainActivity.this, "You have signed in", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
};
if(url.equals(Constants.CARTURL) && isRedirected && isLoggedin.equals("false")) {
Log.e(TAG, "Cart Login");
//webView.loadUrl(Constants.ACCOUNTEDITURL);
SharedPreferences pref = getApplicationContext().getSharedPreferences("Login", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("isLogedin", "true");
editor.commit();
setWelcome();
Toast toast = Toast.makeText(MainActivity.this, "You have signed in", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
};
//if(url.equals(Constants.LOGOUTURL) && isLoggedin.equals("true")) {
if(url.equals(Constants.LOGOUTURL)) {
Log.e(TAG, "logging out-----------------");
SharedPreferences pref = getApplicationContext().getSharedPreferences("Login", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("isLogedin", "false");
//editor.clear();
editor.commit();
setWelcome();
Toast toast = Toast.makeText(MainActivity.this, "You have logged out", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
//return;
}
if(isLoggedin.equals("true")) {
Log.e(TAG, "in get name-----------------");
mainView.addJavascriptInterface(new myJavaScriptInterface(), "CallToAnAndroidFunction");
//String getnamejs = "(document.getElementById('input-firstname').value);";
String getnamejs = "(document.getElementById('app_customer_name').innerHTML);";
mainView.loadUrl("javascript: window.CallToAnAndroidFunction.getUserName" + getnamejs);
}
非常感谢您的帮助,谢谢您提前。
答案 0 :(得分:0)
你的意思是字符串变量isLoggedin总是假的吗?你能提一下在哪里为变量isLoggedin分配值。
答案 1 :(得分:0)
请尝试下面的代码,我用boolean修改了putString。
boolean isLoggedin;
SharedPreferences pref =
getApplicationContext().getSharedPreferences("Login", MODE_PRIVATE);
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e(TAG, "should override url loading" + url);
view.loadUrl(url);
isRedirected = true;
return true;
}
public void onPageFinished(WebView view, String url) {
Log.e(TAG, "on page finished" + url);
isLoggedin = pref.getBoolean(“isLoggedin”, false);
if (url.equals(Constants.ACCOUNTURL) && url.equals(Constants.CARTURL) && !isLoggedin) {
Log.e(TAG, "reload");
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(“isLoggedin", true);
editor.commit();
setWelcome();
Toast toast = Toast.makeText(MainActivity.this, "You have signed in", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Log.e(TAG, "in get name-----------------");
mainView.addJavascriptInterface(new myJavaScriptInterface(), "CallToAnAndroidFunction");
//String getnamejs = "(document.getElementById('input-firstname').value);";
String getnamejs = "(document.getElementById('app_customer_name')[0].innerHTML);";
mainView.loadUrl("javascript: window.CallToAnAndroidFunction.getUserName" + getnamejs);
}
//if(url.equals(Constants.LOGOUTURL) && isLoggedin) {
if (url.equals(Constants.LOGOUTURL)) {
Log.e(TAG, "logging out-----------------");
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLoggedin", false);
//editor.clear();
editor.commit();
setWelcome();
Toast toast = Toast.makeText(MainActivity.this, "You have logged out", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
//return;
}
如果有帮助,请告诉我!