私はユーザーにデバイスのログイン資格情報を保存しようとしています。そのため、ユーザーが毎回ログインする必要がなくなり、アプリを開くたびにログインする必要があります。パスワードフィールドにパスワードが保存されていますが、電子メールフィールドにもパスワードが保存されています。誰かが私に間違っていることを教えてください。以下は私のコードです。デバイスにログイン資格情報を保存する
private static final String PREFS_NAME = "preferences";
//private static String password;
private static final String PREF_UNAME = "email";
private static final String PREF_PASSWORD = "Password";
Encryption mEncryption=null;
private final String DefaultUnameValue = "";
private String UnameValue;
private final String DefaultPasswordValue = "";
private String PasswordValue;
//private EditText editTextUsername;
private EditText editTextEmail;
private EditText editTextPassword;
private Button mSignUpBtn;
private Button mLoginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_login);
mEncryption = new Encryption(getApplicationContext());
editTextEmail = (EditText) findViewById(R.id.login_page_id);
editTextPassword = (EditText) findViewById(R.id.login_page_password);
mSignUpBtn= (Button) findViewById(R.id.login_page_sign_up);
mSignUpBtn.setOnClickListener(this);
mLoginButton = (Button) findViewById(R.id.login_page_button);
mLoginButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == mLoginButton) {
LoginUser();
}
if (v==mSignUpBtn){
Intent intent=new Intent(LoginActivity.this,RegistrationActivity.class);
startActivity(intent);
}
}
private void LoginUser() {
final String email = editTextEmail.getText().toString().trim();
final String password = editTextPassword.getText().toString().trim();
StringRequest postStringRequest = new StringRequest(Request.Method.POST,LOGIN_API,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(LoginActivity.this, response, Toast.LENGTH_LONG).show();
Log.d(TAG,"Reponse Check :"+response);
ModelObject obj = new Gson().fromJson(response, ModelObject.class);
Log.d(TAG,"Object Check :"+obj);
Log.d(TAG,"Object Check :"+ModelObject.class);
Intent intent=new Intent(LoginActivity.this,RegistrationActivity.class);
startActivity(intent);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_LONG).show();
Log.e(TAG,"Error Response Check :"+error);
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
Log.d(TAG,"For email :"+email);
Log.d(TAG,"For password :"+password);
//try {
Log.d(TAG,"My Credentials email URL Encoder: "+(mEncryption.AESEncode(email)));
Log.d(TAG,"My Credentials email URL DECODED: "+(mEncryption.AESDecode(mEncryption.AESEncode(email))));
params.put("data[User][email]",(mEncryption.AESEncode(email)));
Log.d(TAG,"My Credentials pass URL Encoder: "+(mEncryption.AESEncode(password)));
//Log.d(TAG,"My Credentials email URL DECODED: "+(mEncryption.AESDecode(mEncryption.AESEncode(password))));
params.put("data[User][password]",(mEncryption.AESEncode(password)));
Log.d(TAG,"Params :"+params);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/x-www-form-urlencoded");
headers.put("UUID", "test");
headers.put("APPID", "2A192A0C22");
headers.put("USERID", "1");
headers.put("PLATFORM", "Andriod");
headers.put("APP_REQUEST", "1");
return headers;
}
};
//RequestQueue requestQueue = Volley.newRequestQueue(this);
RequestQueue queue = VolleySingleton.getInstance(this).getRequestQueue();
queue.add(postStringRequest);
}
@Override
public void onPause() {
super.onPause();
savePreferences();
}
@Override
public void onResume() {
super.onResume();
loadPreferences();
}
private void savePreferences() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
// Edit and commit
UnameValue = editTextEmail.getText().toString().trim();
PasswordValue = editTextPassword.getText().toString().trim();
//System.out.println("onPause save name: " + UnameValue);
// System.out.println("onPause save password: " + PasswordValue);
editor.putString(PREF_UNAME, UnameValue);
editor.putString(PREF_PASSWORD, PasswordValue);
editor.commit();
}
private void loadPreferences() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
// Get value
UnameValue = settings.getString(PREF_UNAME, DefaultUnameValue);
PasswordValue = settings.getString(PREF_PASSWORD, DefaultPasswordValue);
editTextEmail.setText(UnameValue);
editTextPassword.setText(PasswordValue);
System.out.println("onResume load name: " + UnameValue);
System.out.println("onResume load password: " + PasswordValue);
}
}