-2

I have successfully obtained the data in JSON format from a database which is passed into this Session class to the method setPersonalUser(). For some reason it is not putting the string values i.e. fullName,email, mobile etc. into the preferences hashmap. It works fine for setReceivedUser() but does not work for setPersonalUser() and I don't think setProfessionalUser() either. It is returning no values what so ever. Not even null.

Here is the Session.java class

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

}

And here is the personalHome.java class where the setPersonalUser() and getFullNamePersonal() etc are being called.

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

    }

}
Hazed 2.0
  • 133
  • 2
  • 14
  • 2
    You need to clarify what you mean by "it is not working". You have a lot of debugging statements, so annotate the code with comments about what is happening, like "this prints X", "this is never called", "I expect this to return 'foo' but it's returning 'bar'", etc... Are any exceptions being caught and printed to the log? – Tyler V Aug 14 '18 at 03:10

2 Answers2

1

Reolved. In the JSON label is noticed I mispelt website as 'wesbite' which is why it wasn't working.Sorry for wasting everyone's time.

Hazed 2.0
  • 133
  • 2
  • 14
0

The problem is that you are calling edit on the shared preferences variable and you are not calling apply. SharedPreference is used to get values and SharedPreferences.Editor is used to save values. Create a SharedPreferences.Editor, use it to save some stuff, then call apply (commit also works, but apply is recommended) on the editor.

// Get SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("com.company.app", Context.MODE_PRIVATE);
// Get a value from SharedPreferences
String id = sharedPreferences.getString("id","");
// Get Editor
SharedPreferences.Editor editor = sharedPreferences.edit();
// Use editor to add data and save it to SharedPreferences
editor.putString("id", id);
editor.apply();
Raymond Mutyaba
  • 950
  • 1
  • 9
  • 14
  • Ok i've changed it, still doesn't work. https://pastebin.com/Tx9TNqXi The log still returns no value for `session.getFullNamePersonal();` – Hazed 2.0 Aug 14 '18 at 16:03
  • System.out.println() (Line 127 & 161) is used in normal Java programs. Log.d(String tag, String message) is in Android apps to print out to the Logcat in Android Studio. https://stackoverflow.com/questions/7959263/android-log-v-log-d-log-i-log-w-log-e-when-to-use-each-one – Raymond Mutyaba Aug 14 '18 at 19:41
  • Also be careful with how you use JSON Objects. I have had JSON Objects which contained JSON Arrays, and each JSON Array had a JSON Object. So I had to get each array from the larger object and set them to a JSON Object, before calling getString for each item. Copy and paste some JSON into a JSON lint ( https://jsonlint.com ) to see what the structure of your JSON Object looks like. You might have some JSON Arrays in there you need to unpack first. – Raymond Mutyaba Aug 14 '18 at 19:55
  • I have put the JSON through the lint and it's valid. Here is my JSON `{"id":"12","full_name":"josh jamrs","mobile_no":"test","email":"josh@nowswap.com","job_title":"test","company":"test","website":"test"}` – Hazed 2.0 Aug 14 '18 at 20:54
  • I have now reararnged my `Session.java` to a new way of doing it. It prints values for all the system.out except for `System.out.println("line63 "+website);` for the website value where it does not even print anything. https://pastebin.com/GdA0URnq This is a very strange phenomonon. – Hazed 2.0 Aug 14 '18 at 20:58