簡體   English   中英

將JSON插入SQLite(GSON + Volley)

[英]Insert JSON into SQLite (GSON + Volley)

來自這個主題:將Gson數據插入Android Sqlite

從Volley的onResponse開始:

        public void onResponse(String response) {
        OverallModel itemList;
        itemList = new Gson().fromJson(response,OverallModel[].class);
        helper.insertData(itemList);

這是我的總體模型:

public class OverallModel {
    public int id;
    public String beschreibung;
    public String datum;
    public String image_link;
    public String location;

    public int getId() {
        return id;
    }

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

    public String getBeschreibung() {
        return beschreibung;
    }

    public void setBeschreibung(String beschreibung) {
        this.beschreibung = beschreibung;
    }

... other getters and setters here

使用這樣的JSON:

[
  {
    "id": "1",
    "name": "Event One",
    "beschreibung": "Description here - Skip quot'e's'?",
    "date": "14.01.2017",
    "image_link": "http://google.com/icon.png"
    "location":"location goes here"
  },
  {
    "id": "2",
    "name": "Event Two",
    "beschreibung": "Description here - Skip quot'e's'?",
    "date": "16.01.2017",
    "image_link": "http://google.com/icon.png"
    "location":"location goes here"
  },
  .
  .
  .
]

現在,我需要一個輔助方法來將數據放入SQLite DB中,而無需GSON,我使用以下代碼進行了處理:

JSONArray events = response.getJSONArray("events");
for (int h = 0; h < events.length(); h++) {
String jr_date = c.getString("datum");
String jr_imgurl = c.getString("image_link");
.
.
String SQLiteDataBaseQuery
                                = "INSERT INTO overall_table (id,des,name,date,imgurl,fburl,fbid,entry,open,closed) VALUES('"+id+"', '"+jr_descr+"', '"+jr_name+"', '"+jr_date+"', '"+jr_imgurl+"', '"+jr_fburl+"', '"+jr_fbid+"', '8.0' , '22:00', '3:30');";
                        sqLiteDatabase.execSQL(SQLiteDataBaseQuery);

使用數據庫幫助程序類:

public void onCreate(SQLiteDatabase database) {
String CREATE_TABLE="CREATE TABLE IF NOT EXISTS " +TAB_NAME+" ("+table_id+" INTEGER PRIMARY KEY, "
+table_desc+" VARCHAR, "
+ table_name+ " VARCHAR, "
+ table_date+" VARCHAR, "
.
.
+ table_open+" VARCHAR,"
+ table_closed+" VARCHAR)";
database.execSQL(CREATE_TABLE);
}

如何為我的GSON創建正確的Helper類,以將反序列化的數據放入到Table / DB中?

使用會議室 ORM數據庫

您可以輕松有效地插入模型列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM