S
S
ScarletPhoenix75262019-03-10 11:01:18
Java
ScarletPhoenix7526, 2019-03-10 11:01:18

Need to save nested JSON strings from server to SQLite. How to do?

I receive data like this:

{
    "dur": 50,
    "inter": 600,
    "time_start": 54,
    "time_end": 108,
    "lunch_s": 780,
    "lunch_e": 840,
    "workweek": {
        "1": "Mon",
        "2": "Tue"
    },
    "delay": 150
}

The request body looks like this:
public class BodyUserSettings implements Serializable {

@SerializedName("dur")
@Expose
private String duration;
@SerializedName("inter")
@Expose
private String intervals;
@SerializedName("time_start")
@Expose
private String timeWorkStart;
@SerializedName("time_end")
@Expose
private String timeWorkEnd;
@SerializedName("lunch_s")
@Expose
private String lunchStart;
@SerializedName("lunch_e")
@Expose
private String lunchEnd;
@SerializedName("workweek")
@Expose
private Workweek workweek;
@SerializedName("delay")
@Expose
private String timeDelay;

public String getDuration() {
    return duration;
}

public void setDuration(String duration) {
    this.duration = duration;
}

public BodyUserSettings withDuration(String duration) {
    this.duration = duration;
    return this;
}

public String getIntervals() {
    return intervals;
}

public void setIntervals(String intervals) {
    this.intervals = intervals;
}

public BodyUserSettings withIntervals(String intervals) {
    this.intervals = intervals;
    return this;
}

public String getTimeWorkStart() {
    return timeWorkStart;
}

public void setTimeWorkStart(String timeWorkStart) {
    this.timeWorkStart = timeWorkStart;
}

public BodyUserSettings withTimeWorkStart(String timeWorkStart) {
    this.timeWorkStart = timeWorkStart;
    return this;
}

public String getTimeWorkEnd() {
    return timeWorkEnd;
}

public void setTimeWorkEnd(String timeWorkEnd) {
    this.timeWorkEnd = timeWorkEnd;
}

public BodyUserSettings withTimeWorkEnd(String timeWorkEnd) {
    this.timeWorkEnd = timeWorkEnd;
    return this;
}

public String getLunchStart() {
    return lunchStart;
}

public void setLunchStart(String lunchStart) {
    this.lunchStart = lunchStart;
}

public BodyUserSettings withLunchStart(String lunchStart) {
    this.lunchStart = lunchStart;
    return this;
}

public String getLunchEnd() {
    return lunchEnd;
}

public void setLunchEnd(String lunchEnd) {
    this.lunchEnd = lunchEnd;
}

public BodyUserSettings withLunchEnd(String lunchEnd) {
    this.lunchEnd = lunchEnd;
    return this;
}

public Workweek getWorkweek() {
    return workweek;
}

public void setWorkweek(Workweek workweek) {
    this.workweek = workweek;
}

public BodyUserSettings withWorkweek(Workweek workweek) {
    this.workweek = workweek;
    return this;
}

public String getTimeDelay() {
    return timeDelay;
}

public void setTimeDelay(String timeDelay) {
    this.timeDelay = timeDelay;
}

public BodyUserSettings withTimeDelay(String timeDelay) {
    this.timeDelay = timeDelay;
    return this;
}

public class Workweek implements Serializable {

    @SerializedName("1")
    @Expose
    private String _1;
    @SerializedName("2")
    @Expose
    private String _2;

    public Workweek(String _1, String _2) {
        super();
        this._1 = _1;
        this._2 = _2;
    }

    public String getId() {
        return _1;
    }

    public void setId(String id) {
        this._1 = id;
    }

    public Workweek withId(String _1) {
        this._1 = _1;
        return this;
    }

    public String getTitle() {
        return _2;
    }

    public void setTitle(String _2) {
        this._2 = _2;
    }

    public Workweek withTitle(String _2) {
        this._2 = _2;
        return this;
    }
}
}

POST request:
@POST("/settings")
Call<BodyUserSettings> postIdToken(@Body BodyIdToken id_token);

In mainactivity:
private void sendRequestIdTokenToServer() {
    BodyIdToken hexPost = new BodyIdToken();
    hexPost.idToken = mIdToken;
    Call<BodyUserSettings> call = mService.postIdToken(hexPost);
    call.enqueue(new Callback<BodyUserSettings>() {
        @Override
        public void onResponse(@NonNull Call<BodyUserSettings> call, @NonNull Response<BodyUserSettings> response) {
            mBodyUserSettings = response.body();
            mMainDatabase.saveSettings(mBodyUserSettings);
            mMainDatabase.saveWorkweekDays(mBodyWorkweek);
        }

        @Override
        public void onFailure(@NonNull Call<BodyUserSettings> call, @NonNull Throwable t) {

        }
    });
}

Tables in SQLite:
private final String CREATE_SETTINGS_TABLE = "CREATE TABLE IF NOT EXISTS " +
            SETTINGS_TABLE + "(" +
            _id + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            DURATION + "INTEGER," +
            INTERVALS + " INTEGER," +
            TIME_START + " INTEGER," +
            TIME_END + " INTEGER," +
            LAUNCH_S + " INTEGER," +
            LAUNCH_E + " INTEGER," +
            WORKWEEK + " INTEGER," +
            DELAY + " INTEGER" + ")";

    private final String CREATE_WORKWEEK_TABLE = "CREATE TABLE IF NOT EXISTS " +
            WORKWEEK_TABLE + "(" +
            _id + " INTEGER PRIMARY KEY," +
            ID_DAY + " TEXT," +
            TITLE_DAY + " TEXT" + ")";

Saving to SQLite
public void saveSettings(BodyUserSettings settings) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();

    cv.put(DURATION, settings.getDuration());
    cv.put(INTERVALS, settings.getIntervals());
    cv.put(TIME_START, settings.getTimeWorkStart());
    cv.put(TIME_END, settings.getTimeWorkEnd());
    cv.put(LAUNCH_S, settings.getLunchStart());
    cv.put(LAUNCH_E, settings.getLunchEnd());
    cv.put(WORKWEEK, String.valueOf(settings.getWorkweek()));
    cv.put(DELAY, settings.getTimeDelay());

    db.insert(SETTINGS_TABLE, null, cv);
    Log.d("LOG_TAG_saveSettings ", "save: " +
            settings.getDuration() + "; " +
            settings.getIntervals() + "; " +
            settings.getTimeWorkStart() + "; " +
            settings.getTimeWorkEnd() + "; " +
            settings.getLunchStart() + "; " +
            settings.getLunchEnd() + "; " +
            settings.getWorkweek() + "; " +
            settings.getTimeDelay() + ".");
    db.close();
}

public void saveWorkweekDays(BodyUserSettings.Workweek workweek) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();

    cv.put(ID_DAY, workweek.getId());
    cv.put(TITLE_DAY, workweek.getTitle());

    db.insert(WORKWEEK_TABLE, null, cv);
    Log.d("LOG_TAG_saveWorkweek", "save " +
            workweek.getId() + "; " +
            workweek.getTitle() + ".");
    db.close();
}
}

I don’t understand how I can correctly display data from workweek. Maybe they don't save at all. What am I doing wrong?
If you use JSON parsing, then how to do it and then save it in SQLite?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question