Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在响应方法中使用SharedReferences?_Android_Json_Sharedpreferences_Retrofit2_Android Sharedpreferences - Fatal编程技术网

Android 如何在响应方法中使用SharedReferences?

Android 如何在响应方法中使用SharedReferences?,android,json,sharedpreferences,retrofit2,android-sharedpreferences,Android,Json,Sharedpreferences,Retrofit2,Android Sharedpreferences,我试图在用户登录时获取其登录凭据。我向我的数据库用户发送电子邮件和用户密码,然后返回一个Json,如下所示。其实问题很简单,但我不知道解决办法,我是个新手我百分之百确定onResponse方法中的错误。因为我看到了这个成功的响应,所以我得到了成功的响应,但这里的问题是我不知道如何将这个Json响应转换成SharedReferences。谢谢 { "User": [ { "SSN": "123456789", "FirstNa

我试图在用户登录时获取其登录凭据。我向我的数据库用户发送电子邮件和用户密码,然后返回一个Json,如下所示。其实问题很简单,但我不知道解决办法,我是个新手我百分之百确定onResponse方法中的错误。因为我看到了这个成功的响应,所以我得到了成功的响应,但这里的问题是我不知道如何将这个Json响应转换成SharedReferences。谢谢

{
    "User": [
        {
            "SSN": "123456789",
            "FirstName": "Furkan",
            "User_Role": "1"
        }
    ]
}
我的模型类是:

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中的SharedReference中。但我想不出来。

以下是我的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();
            }
        });

    }
}
公共类LoginActivity扩展AppCompatActivity实现View.OnClickListener{
按钮bSignUp,bLogin;
编辑文本etPassword,etEmail;
私有int userRoleTypeId;
私人共享引用共享引用;
私有进程对话;
意图;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
bSignUp=(按钮)findViewById(R.id.bSignUp);
bLogin=(按钮)findviewbyd(R.id.bLogin);
bSignUp.setOnClickListener((View.OnClickListener)this);
setOnClickListener((View.OnClickListener)this);
etPassword=(EditText)findViewById(R.id.etPassword);
etEmail=(EditText)findViewById(R.id.etEmail);
progressDialog=新建progressDialog(此);
progressDialog.setTitle(“登录”);
progressDialog.setMessage(“请稍候…”);
SharedReferences=getApplicationContext().GetSharedReferences(“LoginActivity”,MODE_PRIVATE);
setOnClickListener(this);
}
@凌驾
公共void onClick(视图){
开关(view.getId()){
案例R.id.B签名:
Intent SignUpIntent=新的Intent(这个,SignUpActivity.class);
startActivity(注册意向);
打破
案例R.id.bLogin:
字符串email=etEmail.getText().toString();
字符串password=etPassword.getText().toString();
如果(!email.isEmpty()&&!password.isEmpty()){
progressDialog.show();
登录过程(电子邮件、密码);
this.userRoleTypeId=sharedPreferences.getInt(Constants.USERROLE,0);
if(userRoleTypeId==1){
意图=新意图(此,OrganizerHomeActivity.class);
星触觉(意向);
}else if(userRoleTypeId==2 | | userRoleTypeId==3){
intent=新的intent(这个,UserHomeActivity.class);
星触觉(意向);
}
否则{
Toast.makeText(LoginActivity.this,“错误的电子邮件或密码”,Toast.LENGTH_SHORT.show();
}
}否则{
Toast.makeText(LoginActivity.this,“字段为空”,Toast.LENGTH_SHORT.show();
}
打破
违约:
打破
}
}
私有void登录过程(字符串电子邮件、字符串密码){
LoginAPI LoginAPI=reformationservice.getClient().create(LoginAPI.class);
2.Call Call=loginAPI.loginUser(电子邮件、密码);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
//我确信这是个错误。在下面我尝试获取Json响应并将其放入共享首选项。
UserList userLists=response.body().getUser();
if(response.issusccessful()){
SharedReferences.Editor=SharedReferences.edit();
putBoolean(Constants.IS_LOGGED_IN,true);
//putString(Constants.NAME,userLists.getFirstName);
//putString(Constants.SSN,userLists.getsSN);
//putInt(Constants.USERROLE,userLists.getuserRoleTypeId);
editor.apply();
//我看到了这个成功的响应,所以我得到了成功的响应,但是这里的问题是我不知道如何将这个Json响应放入SharedRef。
Toast.makeText(LoginActivity.this,“成功响应”,Toast.LENGTH_SHORT.show();
}
progressDialog.disclose();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(LoginActivity.this,t.getMessage(),Toast.LENGTH_SHORT.show();
progressDialog.disclose();
}
});
}
}
试试这个

    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();
            }
if(response.issusccessful()){
List userLists=response.body().getUser();
SharedReferences设置=获取SharedReferences(“首选”,模式\私有);
SharedReferences.Editor prefEditor=settings.edit();
putBoolean(Constants.IS_LOGGED_IN,true);
putString(“用户名”,userLists.getUser().getFirstName());
putString(Constants.SSN,userLists.getUser().getsSN());
putInt(Constants.USERROLE,userLists.getUser().getUserRoleTypeId());
提交();
}

SharedReferences与
改装有什么关系?只需为
SharedReferences
创建一个实用程序类并执行CRUD。但我仍然需要知道如何获取json输出您确定UserList.getUser().get(0).getFirstName()正确吗?因为它无法到达userLists.getUser()方法,表示符号不能解析。谢谢,现在它可以正常工作,除了userLists.getUser().getUserRoleTypeId()。在Logcat中