Answer the question
In order to leave comments, you need to log in
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
}
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("/settings")
Call<BodyUserSettings> postIdToken(@Body BodyIdToken id_token);
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) {
}
});
}
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" + ")";
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();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question