在设备中保存登录凭据

时间:2017-03-20 07:06:25

标签: android login sharedpreferences

我正在尝试将用户的登录凭据保存到设备,这样用户每次打开app时都不需要登录。所以,我在这里遇到的问题是在密码字段中密码是存储,但在电子邮件领域,密码也被存储。有人请告诉我我做错了什么?下面是我的代码。

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);
   }
}

2 个答案:

答案 0 :(得分:0)

保存登录成功的首选项(onResponse of Volley)并在onResume或onCreate中加载首选项。

答案 1 :(得分:0)

在onResponse中调用savePreferences()方法。从onPause中删除savePreferences()。

            @Override
            public void onResponse(String response) {
             savePreferences();
                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);

                }
相关问题