如何在 Retrofit2 onResponse 方法中使用 SharedPreferences?

How to Use SharedPreferences in Retrofit2 onResponse method?

我试图在用户登录时获取他的登录凭据。我将发送到我的数据库 User_Email 和 User_Password,然后返回 Json,如下所示。其实问题很简单,但我不知道解决方案,我有点新。 我 %100 确定我的 onResponse 方法中有错误。因为我看到了这个成功的响应 Toast,所以我得到了成功的响应 但是这里的问题我不知道如何将这个 Json 响应放入共享首选项中。谢谢.

{
    "User": [
        {
            "SSN": "123456789",
            "FirstName": "Furkan",
            "User_Role": "1"
        }
    ]
}

我的模型 class 此响应是:

public class UserList {

    @SerializedName("User")
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

public class User {
    @SerializedName("SSN")
    public String sSN;
    @SerializedName("FirstName")
    public String firstName;
    @SerializedName("User_Role")
    public intuserRole;

    public String getsSN() {
        return sSN;
    }

    public void setsSN(String sSN) {
        this.sSN = sSN;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public int getUserRoleTypeId() {
        return userRoleTypeId;
    }

    public void setUserRoleTypeId(int userRoleTypeId) {
        this.userRoleTypeId = userRoleTypeId;
    }
}

我正在尝试将这些 userRoleTypeId、firstName 和 sSN 添加到我的 call.enqueue onResponse 中的 SharedPreferences。但是我想不通。

这是我的 LoginActivity.class 登录信息:

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    Button bSignUp, bLogin;
    EditText etPassword, etEmail;
    private int userRoleTypeId;
    private SharedPreferences sharedPreferences;
    private ProgressDialog progressDialog;
    Intent intent;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        bSignUp = (Button) findViewById(R.id.bSignUp);
        bLogin = (Button) findViewById(R.id.bLogin);
        bSignUp.setOnClickListener((View.OnClickListener) this);
        bLogin.setOnClickListener((View.OnClickListener) this);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etEmail = (EditText) findViewById(R.id.etEmail);
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Logging in");
        progressDialog.setMessage("Please wait ....");

        sharedPreferences = getApplicationContext().getSharedPreferences("LoginActivity",MODE_PRIVATE);

 bLogin.setOnClickListener(this);
    }

 @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bSignUp:
                Intent SignUpIntent = new Intent(this, SignUpActivity.class);
                startActivity(SignUpIntent);
                break;
            case R.id.bLogin:
                String email = etEmail.getText().toString();
                String password = etPassword.getText().toString();
                if (!email.isEmpty() && !password.isEmpty()) {
                    progressDialog.show();
                    loginProcess(email, password);
                    this.userRoleTypeId = sharedPreferences.getInt(Constants.USERROLE, 0);
                    if (userRoleTypeId == 1) {
                        intent = new Intent(this, OrganizerHomeActivity.class);
                        startActivity(intent);
                    } else if(userRoleTypeId == 2 || userRoleTypeId == 3) {
                        intent = new Intent(this, UserHomeActivity.class);
                        startActivity(intent);
                    }
                    else{
                        Toast.makeText(LoginActivity.this, "Wrong email or password", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "Fields are empty", Toast.LENGTH_SHORT).show();
                }

                break;
            default:
                break;
        }
    }

 private void loginProcess(String email, String password) {
        LoginAPI loginAPI = RetrofitService.getClient().create(LoginAPI.class);
        retrofit2.Call<UserList> call = loginAPI.loginUser(email,password);
        call.enqueue(new Callback<UserList>(){
            @Override
            public void onResponse(Call<UserList> call, Response<UserList> response) {
                //I am sure the fault in here. In below i tried to take Json response and fetch it into the Shared Preferences.
                UserList userLists = response.body().getUser();
                if(response.isSuccessful()){
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean(Constants.IS_LOGGED_IN, true);
                   // editor.putString(Constants.NAME, userLists.getFirstName);
                    //editor.putString(Constants.SSN, userLists.getsSN);
                   // editor.putInt(Constants.USERROLE, userLists.getuserRoleTypeId);
                    editor.apply();


    // I am seeing this successfull response toast, so i am getting successfull response but the problem here i dont know how to get this Json response into sharedpref.
Toast.makeText(LoginActivity.this, "successfull response", Toast.LENGTH_SHORT).show();
                }
                progressDialog.dismiss();
            }

            @Override
            public void onFailure(Call<UserList> call, Throwable t) {
                Toast.makeText(LoginActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        });

    }
}

试试这个

    if(response.isSuccessful()){
                List<User> userLists = response.body().getUser();
    SharedPreferences settings = getSharedPreferences("Pref", MODE_PRIVATE);
    SharedPreferences.Editor prefEditor = settings.edit();
    editor.putBoolean(Constants.IS_LOGGED_IN, true);
    prefeditor.putString("UserName", userLists.getUser().getFirstName());
    prefEditor.putString(Constants.SSN, userLists.getUser().getsSN());
    prefEditor.putInt(Constants.USERROLE, userLists.getUser().getUserRoleTypeId());
    prefEditor.commit();
            }