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