Ahmad
Ahmad

Reputation: 201

save OBJECT in preference and also retrieved it

jsoncofigdata = "{\n" +
            "  \"configrations\": [\n" +
            "    {\n" +
            "      isofflineserviceRunning: true,\n" +
            "      ispushservicerunning: true,\n" +
            "      isfakegpsrunning: true,\n" +
            "      ispowersavingmodeenable: true,\n" +
            "      iskmviewvisible: true,\n" +
            "      driveronlinefromconfig: true,\n" +
            "      driverofflinefromconfig: true,\n" +
            "      iscancelridebuttonvisible: true,\n" +
            "      ismonthlyfeatureenable: true\n" +
            "    }\n" +
            "  ]\n" +
            "}";

 try {
        JSONObject jObj = new JSONObject(jsoncofigdata);
        if (jObj.has("configrations")) {
            JSONArray jsonArrray = jObj.getJSONArray("configrations");
            if (jsonArrray.length() > 0) {
                ModelOfFunctionalityOnOff.clear();
                for (int i = 0; i < jsonArrray.length(); i++) {
                    JSONObject configrations = jsonArrray.getJSONObject(i);
                     isOfflineServiceRunning = configrations.getBoolean("isofflineserviceRunning");
                     isPushServiceRunning = configrations.getBoolean("ispushservicerunning");
                     isFakeGpsRunning = configrations.getBoolean("isfakegpsrunning");
                     isPowerSavingModeEnable = configrations.getBoolean("ispowersavingmodeenable");
                     isKmViewVisible = configrations.getBoolean("iskmviewvisible");
                     DriverOnlineFromConfig = configrations.getBoolean("driveronlinefromconfig");
                     DriverOfflineFromConfig = configrations.getBoolean("driverofflinefromconfig");
                     IsCancelRideButtonVisible = configrations.getBoolean("iscancelridebuttonvisible");
                     IsMonthlyfeatureEnable= configrations.getBoolean("ismonthlyfeatureenable");

                    ModelOfFunctionalityOnOff.add(new ModelOfFunctionalityOnOff(
                            configrations.getBoolean("isofflineserviceRunning"),
                            configrations.getBoolean("ispushservicerunning"),
                            configrations.getBoolean("isfakegpsrunning"),
                            configrations.getBoolean("ispowersavingmodeenable"),
                            configrations.getBoolean("iskmviewvisible"),
                            configrations.getBoolean("driveronlinefromconfig"),
                            configrations.getBoolean("driverofflinefromconfig"),
                            configrations.getBoolean("iscancelridebuttonvisible"), configrations.getBoolean("ismonthlyfeatureenable")));

                    // Log.d("json", "" + textValues);

                }
                SharedPreferences pref = getSharedPreferences(Data.SHARED_PREF_NAME, 0);
                SharedPreferences.Editor prefsEditor = pref.edit();
                Gson gson = new Gson();
                String json = gson.toJson(jsoncofigdata);
                prefsEditor.putString("configrations", json);
                prefsEditor.commit();

            }

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

Data saved but when i retreived Object it through error.

Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $

SharedPreferences pref = getSharedPreferences(Data.SHARED_PREF_NAME, 0);

    Gson gson = new Gson();
    String json = pref.getString("configrations", "");
    ModelOfFunctionalityOnOff obj = gson.fromJson(json, ModelOfFunctionalityOnOff.class);

may be this line of code through error

ModelOfFunctionalityOnOff obj = gson.fromJson(json, ModelOfFunctionalityOnOff.class);

Model Class

public class ModelOfFunctionalityOnOff {
Boolean isOfflineServiceRunning;
Boolean isPushServiceRunning;
Boolean isFakeGpsRunning;
Boolean isPowerSavingModeEnable;
Boolean isKmViewVisible;
Boolean DriverOnlineFromConfig;
Boolean DriverOfflineFromConfig;
Boolean IsCancelRideButtonVisible;

public Boolean getOfflineServiceRunning() {
    return isOfflineServiceRunning;
}

public void setOfflineServiceRunning(Boolean offlineServiceRunning) {
    isOfflineServiceRunning = offlineServiceRunning;
}

public Boolean getPushServiceRunning() {
    return isPushServiceRunning;
}

public void setPushServiceRunning(Boolean pushServiceRunning) {
    isPushServiceRunning = pushServiceRunning;
}

public Boolean getFakeGpsRunning() {
    return isFakeGpsRunning;
}

public void setFakeGpsRunning(Boolean fakeGpsRunning) {
    isFakeGpsRunning = fakeGpsRunning;
}

public Boolean getPowerSavingModeEnable() {
    return isPowerSavingModeEnable;
}

public void setPowerSavingModeEnable(Boolean powerSavingModeEnable) {
    isPowerSavingModeEnable = powerSavingModeEnable;
}

public Boolean getKmViewVisible() {
    return isKmViewVisible;
}

public void setKmViewVisible(Boolean kmViewVisible) {
    isKmViewVisible = kmViewVisible;
}

public Boolean getDriverOnlineFromConfig() {
    return DriverOnlineFromConfig;
}

public void setDriverOnlineFromConfig(Boolean driverOnlineFromConfig) {
    DriverOnlineFromConfig = driverOnlineFromConfig;
}

public Boolean getDriverOfflineFromConfig() {
    return DriverOfflineFromConfig;
}

public void setDriverOfflineFromConfig(Boolean driverOfflineFromConfig) {
    DriverOfflineFromConfig = driverOfflineFromConfig;
}

public Boolean getCancelRideButtonVisible() {
    return IsCancelRideButtonVisible;
}

public void setCancelRideButtonVisible(Boolean cancelRideButtonVisible) {
    IsCancelRideButtonVisible = cancelRideButtonVisible;
}

public Boolean getMonthlyFeatureEnable() {
    return IsMonthlyFeatureEnable;
}

public void setMonthlyFeatureEnable(Boolean monthlyFeatureEnable) {
    IsMonthlyFeatureEnable = monthlyFeatureEnable;
}

public ModelOfFunctionalityOnOff(Boolean isOfflineServiceRunning, Boolean isPushServiceRunning, Boolean isFakeGpsRunning, Boolean isPowerSavingModeEnable, Boolean isKmViewVisible, Boolean driverOnlineFromConfig, Boolean driverOfflineFromConfig, Boolean isCancelRideButtonVisible, Boolean isMonthlyFeatureEnable) {
    this.isOfflineServiceRunning = isOfflineServiceRunning;
    this.isPushServiceRunning = isPushServiceRunning;
    this.isFakeGpsRunning = isFakeGpsRunning;
    this.isPowerSavingModeEnable = isPowerSavingModeEnable;
    this.isKmViewVisible = isKmViewVisible;
    DriverOnlineFromConfig = driverOnlineFromConfig;
    DriverOfflineFromConfig = driverOfflineFromConfig;
    IsCancelRideButtonVisible = isCancelRideButtonVisible;
    IsMonthlyFeatureEnable = isMonthlyFeatureEnable;
}

Boolean IsMonthlyFeatureEnable;

  }

Upvotes: 0

Views: 83

Answers (2)

Sanjay Kumar
Sanjay Kumar

Reputation: 1185

To save your object, Let say LocationBean -

 String locationJson = new Gson().toJson(locationBean, LocationBean.class);
SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(_context);
            SharedPreferences.Editor editor = spref.edit();
            editor.putString(your_key, locationJson);
            editor.apply();

To fetch object again -

       SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(_context);
    String locationJson  = pref.getString(your_key, defaulValue);
 LocationBean locationBean = new Gson().fromJson(locationJson, LocationBean.class);

Upvotes: 0

Lance Toth
Lance Toth

Reputation: 440

In this line String json = gson.toJson(jsoncofigdata); you're converting a json to json. You could just add this variable to your Preferences like prefsEditor.putString("configrations", jsoncofigdata);

Upvotes: 1

Related Questions