JSONObject / SharedPreferences无法检索字符串

时间:2018-08-14 02:23:35

标签: java android sharedpreferences

我已经成功地从数据库中获取了JSON格式的数据,该数据库已传递到此Session类中,并传递给方法setPersonalUser()。由于某些原因,它没有将字符串值(即fullName,email,mobile等)放入首选项hashmap中。它对setReceivedUser()很好用,但对setPersonalUser()不起作用,我也不认为setProfessionalUser()也是。它从未返回任何值。甚至都不为空。

这是Session.java类

public class Session {

    private SharedPreferences preferences;

    public Session(Context context) {

        preferences = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public void setUsername(String username) {
        preferences.edit().putString("username", username).commit();
    }

    public void setId(String id) {

        preferences.edit().putString("id", id).commit();
    }

    public void setReceivedId(String id ){
        preferences.edit().putString("received_id", id).commit();
    }

    public String getUsername() {
        String username = preferences.getString("username","");
        return username;
    }

    public String getId(){
        String id = preferences.getString("id","");
        return id;
    }

    public String getReceivedId(){
        String receivedId = preferences.getString("received_id","");
        return receivedId;
    }
    //method to store incoming user info
    public void setReceivedUser(String data){
        try {
            JSONObject jsonObject = new JSONObject(data);
            String fullName = jsonObject.getString("full_name");
            System.out.println("json object line42 "+fullName);
            String email = jsonObject.getString("email");
            String mobile = jsonObject.getString("mobile_no");
            String jobTitle = jsonObject.getString("job_title");
            String company = jsonObject.getString("company");
            String website = jsonObject.getString("website");
            preferences.edit().putString("full name", fullName).commit();
            preferences.edit().putString("email", email).commit();
            preferences.edit().putString("mobile no", mobile).commit();
            preferences.edit().putString("job title", jobTitle).commit();
            preferences.edit().putString("company", company).commit();
            preferences.edit().putString("website", website).commit();
        }catch (JSONException e){
            e.printStackTrace();
        }

    }
    public String getFullName(){
        String fullName = preferences.getString("full name", "");
        return fullName;
    }

    public String getEmail(){
        String email = preferences.getString("email", "");
        return email;
    }

    public String getMobileNo(){
        String mobile = preferences.getString("mobile no","");
        return mobile;
    }

    public String getJobTitle(){
        String jobTitle = preferences.getString("job title","");
        return jobTitle;
    }

    public String getCompany(){
        String company = preferences.getString("company","");
        return company;
    }

    public String getWebsite(){
        String website = preferences.getString("website", "");
        return website;
    }

//All the stuff below here is NOT working        
    public void setPersonalUser(String data){
        try {
            JSONObject jsonObject = new JSONObject(data);
            String fullName = jsonObject.getString("full_name");
            System.out.println("json objects line102 "+ data);
            String email = jsonObject.getString("email");
            String mobile = jsonObject.getString("mobile_no");
            String jobTitle = jsonObject.getString("job_title");
            String company = jsonObject.getString("company");
            String website = jsonObject.getString("website");
            preferences.edit().putString("full name personal", fullName).commit();
            preferences.edit().putString("email personal", email).commit();
            preferences.edit().putString("mobile no personal", mobile).commit();
            preferences.edit().putString("job title personal", jobTitle).commit();
            preferences.edit().putString("company personal", company).commit();
            preferences.edit().putString("website personal", website).commit();
        }catch (JSONException e){
            e.printStackTrace();
        }

    }

    public String getFullNamePersonal(){
        String fullName = preferences.getString("full name personal", "");
        System.out.println("line 122 session "+fullName);
        return fullName;
    }

    public String getEmailPersonal(){
        String email = preferences.getString("email personal", "");
        return email;
    }

    public String getMobileNoPersonal(){
        String mobile = preferences.getString("mobile no personal","");
        return mobile;
    }

    public String getJobTitlePersonal(){
        String jobTitle = preferences.getString("job title personal","");
        return jobTitle;
    }

    public String getCompanyPersonal(){
        String company = preferences.getString("company personal","");
        return company;
    }

    public String getWebsitePersonal(){
        String website = preferences.getString("website personal","");
        return website;
    }

    public void setProfessionalUser(String data){
        try {
            JSONObject jsonObject = new JSONObject(data);
            String fullName = jsonObject.getString("full_name");
            System.out.println("json object line42 "+fullName);
            //full name is printed successfully
            String email = jsonObject.getString("email");
            String mobile = jsonObject.getString("mobile_no");
            String jobTitle = jsonObject.getString("job_title");
            String company = jsonObject.getString("company");
            String website = jsonObject.getString("website");
            preferences.edit().putString("full name professional", fullName).commit();
            preferences.edit().putString("email professional", email).commit();
            preferences.edit().putString("mobile no professional", mobile).commit();
            preferences.edit().putString("job title professional", jobTitle).commit();
            preferences.edit().putString("company professional", company).commit();
            preferences.edit().putString("website professional", website).commit();
        }catch (JSONException e){
            e.printStackTrace();
        }

    }

    public String getFullNameProfessional(){
        String fullName = preferences.getString("full name professional", "");
        return fullName;
    }

    public String getEmailProfessional(){
        String email = preferences.getString("email professional", "");
        return email;
    }

    public String getMobileNoProfessional(){
        String mobile = preferences.getString("mobile no professional","");
        return mobile;
    }

    public String getJobTitleProfessional(){
        String jobTitle = preferences.getString("job title professional","");
        return jobTitle;
    }

    public String getCompanyProfessional(){
        String company = preferences.getString("company professional","");
        return company;
    }

    public String getWebsiteProfessional(){
        String website = preferences.getString("website professional", "");
        return website;
    }

}

这是personalHome.java类,其中正在调用setPersonalUser()getFullNamePersonal()等。

public class personalHome extends AppCompatActivity  {


    @Override
    @TargetApi(25)
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GetProfileInfo getProfileInfo = new GetProfileInfo();
        setContentView(R.layout.personal_home);
        onResume();
        LinearLayout l1 = (LinearLayout) findViewById(R.id.linlay1);
        View edit = l1.findViewById(R.id.editInfo);

        //the code here retrieves the selected profile picture if it exists and loads it

        Session session = new Session(getApplicationContext());

        final String userId = session.getId();
        final String receivedId = session.getReceivedId();
        try {
            session = new Session(getApplicationContext());
            String data = getProfileInfo.execute(userId, "personal").get();
            Log.i("line73", data);
            //the JSON data is successfully logged to console 
            session.setPersonalUser(data);
            Log.i("line75", session.getFullNamePersonal());
           //this log statement returns no value
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        catch(ExecutionException e){
            e.printStackTrace();
        }

        String nameValue = session.getFullNamePersonal();
        TextView tvFullName = (TextView) findViewById(R.id.fullname);
        tvFullName.setText(nameValue);
        Log.i("Full Name", nameValue);

        String mobileNoValue = session.getMobileNoPersonal();
        TextView tvMobile = (TextView) findViewById(R.id.mobileno);
        tvMobile.setText(mobileNoValue);
        Log.i("Mobile No", mobileNoValue);

        String emailValue = session.getEmailPersonal();
        TextView tvEmail = (TextView) findViewById(R.id.email);
        tvEmail.setText(emailValue);
        Log.i("Email", emailValue);

        String jobValue = session.getJobTitlePersonal();
        TextView tvJobtitle = (TextView) findViewById(R.id.jobtitle);
        tvJobtitle.setText(jobValue);

        String companyValue = session.getCompanyPersonal();
        TextView tvCompany = (TextView) findViewById(R.id.company);
        tvCompany.setText(companyValue);
        // tv.setText("hello");

    }

}

2 个答案:

答案 0 :(得分:1)

已解决。在JSON标签中发现我误认为网站为“ wesbite”,这就是为什么它不起作用的原因。很抱歉浪费大家的时间。

答案 1 :(得分:0)

问题是您在共享首选项变量上调用了edit,而没有在apply中调用。 SharedPreference用于获取值,SharedPreferences.Editor用于保存值。创建一个SharedPreferences.Editor,用它保存一些东西,然后在编辑器上调用apply(提交也可以,但是建议应用)。

    <dx-tree-list
        height="40vh"
        ....>