package com.example.dw.dw20181121moni.model;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
@Entity
public class SaveHomeBean {
@Id(autoincrement = true)
private Long id;
private String data;
@Generated(hash = 666561533)
public SaveHomeBean(Long id, String data) {
this.id = id;
this.data = data;
}
@Generated(hash = 1564848766)
public SaveHomeBean() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getData() {
return this.data;
}
public void setData(String data) {
this.data = data;
}
}
package com.example.dw.dw20181121moni.utils;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.example.dw.dw20181121moni.DaoMaster;
import com.example.dw.dw20181121moni.DaoSession;
import com.example.dw.dw20181121moni.model.GreenBean;
import com.example.dw.dw20181121moni.model.SaveHomeBean;
import java.util.List;
public class DWSqutils {
private static DWSqutils ourInstance =null;
private DaoMaster daoMaster;
private DaoSession daoSession;
public static DWSqutils getInstance() {
if (ourInstance == null){
ourInstance = new DWSqutils();
}
return ourInstance;
}
private DWSqutils() {
}
public void init(Context context){
DaoMaster.DevOpenHelper dw = new DaoMaster.DevOpenHelper(context, "dw");
SQLiteDatabase db = dw.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
public void insert(GreenBean greenBean){
daoSession.getGreenBeanDao().insert(greenBean);
}
public void insertHome(SaveHomeBean saveHomeBean){
daoSession.getSaveHomeBeanDao().insert(saveHomeBean);
}
public List<GreenBean> query(){
List<GreenBean> list = daoSession.getGreenBeanDao().loadAll();
return list;
}
public List<SaveHomeBean> queryHome(){
List<SaveHomeBean> list = daoSession.getSaveHomeBeanDao().loadAll();
return list;
}
}
boolean networkAvalible = isNetworkAvalible(context);
if (networkAvalible){
doHttpHome();
}else{
List<SaveHomeBean> saveHomeBeans = DWSqutils.getInstance().queryHome();
String s = saveHomeBeans.get(0).getData();
Gson gson = new Gson();
HomeBean homeBean = gson.fromJson(s, HomeBean.class);
tuijianlist = homeBean.getData().getTuijian().getList();
goodsAdapter.setList(tuijianlist);
}
private void doHttpHome() {
new OkHttp().get(urltwo).setNetCallBack(new OkHttp.NetCallBack() {
@Override
public void onSuccess(String result) {
SaveHomeBean saveHomeBean = new SaveHomeBean();
saveHomeBean.setData(result);
DWSqutils.getInstance().insertHome(saveHomeBean);
Gson gson = new Gson();
HomeBean homeBean = gson.fromJson(result, HomeBean.class);
tuijianlist = homeBean.getData().getTuijian().getList();
goodsAdapter.setList(tuijianlist);
}
});
}