【发布时间】:2017-03-11 06:33:34
【问题描述】:
我已经尝试了其他关于共享偏好的答案中给出的所有可能的解决方案,但仍然无法解决,因此提出了这个问题。
我一直在使用这些文件
Config.java
public class Config {
//URL to our login.php file
public static final String LOGIN_URL = "http://192.168.211.1/Remedcu/login.php";
public static final String KEY_NAME = "Name";
public static final String KEY_EMAIL = "Email";
public static final String KEY_PASSWORD = "Password";
//If server response is equal to this that means login is successful
public static final String LOGIN_SUCCESS = "success";
//Keys for Sharedpreferences
//This would be the name of our shared preferences
public static final String SHARED_PREF_NAME = "iamhere";
//This would be used to store the email of current logged in user
public static String EMAIL_SHARED_PREF = null;
//We will use this to store the boolean in sharedpreference to track user is loggedin or not
public static String LOGGEDIN_SHARED_PREF = "loggedin";
}
和 Login.java
public class Login extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private Vibrator vib;
Animation animShake;
private EditText signupInputName, signupInputPassword;
private TextInputLayout signupInputLayoutName,
signupInputLayoutPassword;
private Button btnSignUp;
ProgressDialog progress;
private static final String LOGIN_URL = "http://192.168.211.1/Remedcu/login.php";
public static final String KEY_NAME = "Username";
public static final String KEY_PASSWORD = "Password";
TextView textView;
private boolean loggedIn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
signupInputLayoutName = (TextInputLayout) findViewById(R.id.signup_input_layout_name);
signupInputLayoutPassword = (TextInputLayout) findViewById(R.id.signup_input_layout_password);
textView = (TextView) findViewById(R.id.textView2);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Login.this, Registration.class);
startActivity(intent);
}
});
signupInputName = (EditText) findViewById(R.id.signup_input_name);
signupInputPassword = (EditText) findViewById(R.id.signup_input_password);
btnSignUp = (Button) findViewById(R.id.btn_signup);
animShake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shakei);
vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
submitForm();
//Intent intent=new Intent(Intent.ACTION_VIEW);
}
});
}
private void submitForm() {
final String Name = signupInputName.getText().toString().trim();
final String Password=signupInputPassword.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST,LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.equalsIgnoreCase("Invalid credentials")) {
Toast.makeText(Login.this, response, Toast.LENGTH_SHORT).show();
}
//String LOGIN_SUCCESS = "success";
if(response.equalsIgnoreCase("success")){
final String Use ="Welcome "+Name;
Toast.makeText(getApplicationContext(),Use,Toast.LENGTH_SHORT).show();
//Creating a shared preference
SharedPreferences sharedPreferences = Login.this.getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sharedPreferences.edit();
//Adding values to editor
editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, true);
editor.putString(Config.EMAIL_SHARED_PREF, Name);
//Saving values to editor
editor.apply();
Toast.makeText(getApplicationContext(), "The data is saved as "+Config.EMAIL_SHARED_PREF,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Login.this, MapsActivity.class);
startActivity(intent);
}//else{
//If the server response is not success
//Displaying an error message on toast
// Toast.makeText(Login.this, "Invalid credentials", Toast.LENGTH_LONG).show();
//}
// Intent intent = new Intent(Registration.this, MainActivity.class);
// startActivity(intent);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Login.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_NAME,Name);
params.put(KEY_PASSWORD,Password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
if (!checkName()) {
signupInputName.setAnimation(animShake);
signupInputName.startAnimation(animShake);
vib.vibrate(120);
return;
}
if (!checkPassword()) {
signupInputPassword.setAnimation(animShake);
signupInputPassword.startAnimation(animShake);
vib.vibrate(120);
return;
}
signupInputLayoutName.setErrorEnabled(false);
signupInputLayoutPassword.setErrorEnabled(false);
// Toast.makeText(getApplicationContext(),
// "You are logged in !!", Toast.LENGTH_SHORT).show();
}
private boolean checkName() {
if (signupInputName.getText().toString().trim().isEmpty()) {
signupInputLayoutName.setErrorEnabled(true);
signupInputLayoutName.setError(getString(R.string.err_msg_name));
signupInputName.setError(getString(R.string.err_msg_required));
return false;
}
signupInputLayoutName.setErrorEnabled(false);
return true;
}
private boolean checkPassword() {
if (signupInputPassword.getText().toString().trim().isEmpty()) {
signupInputLayoutPassword.setError(getString(R.string.err_msg_password));
requestFocus(signupInputPassword);
return false;
}
signupInputLayoutPassword.setErrorEnabled(false);
return true;
}
private static boolean isValidEmail(String email) {
return !TextUtils.isEmpty(email) &&
android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
@Override
protected void onResume() {
super.onResume();
//In onresume fetching value from sharedpreference
SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
//Fetching the boolean value form sharedpreferences
loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);
//If we will get true
if (loggedIn) {
//We will start the Profile Activity
Intent intent = new Intent(Login.this, MapsActivity.class);
startActivity(intent);
}
}}
在我将更改提交到编辑器后的 Login.java 中,我只想检查值是否已保存。
因此下面的代码在那里:
Toast.makeText(getApplicationContext(), "The data is saved as "+Config.EMAIL_SHARED_PREF,Toast.LENGTH_SHORT).show();
但是每当我运行程序,登录后,吐司总是“数据保存为空”
有什么办法吗?
【问题讨论】:
-
那是因为那个变量的值是
null你需要检索存储在那个键下的值。 -
之前我将值设为“名称”,然后 Toast 是“数据保存为名称”
-
正是我的观点。您打印该变量的值而不是存储在
SharedPreferences中的实际值 -
如何访问存储的值?对不起,我是菜鸟。
-
发布的任何答案都会起作用。但是,我强烈建议您下次阅读文档。 developer.android.com/reference/android/content/…
标签: java android android-studio session android-studio-2.1