public class Dao { private final Myopenhelp m; private final SQLiteDatabase db; private String jsonstr; public Dao(Context ctx) { m = new Myopenhelp(ctx); db = m.getWritableDatabase(); } //插入以及删除 public void tianjia(String url,String jsonstr){ db.delete("Use","url=?",new String []{jsonstr}); //然后进行添加 ContentValues values=new ContentValues(); values.put("url",url); values.put("jsonstr",jsonstr); db.insert("Use",null, values); db.close(); } //进行查询,根据url获取json串 public String chaxun(String url){ Cursor use = db.query("Use", null, "url=?", new String[]{url}, null, null, null); while (use.moveToNext()) { jsonstr = use.getString(use.getColumnIndex("jsonstr")); } return jsonstr; }
public class Myopenhelp extends SQLiteOpenHelper { public Myopenhelp(Context context) { super(context, "diyizhou.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { //创建表 db.execSQL("create table Use(id integer primary key,url text not null,jsonstr text not null)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { }
Dao dao=new Dao(MainActivity.this); String chaxun = dao.chaxun(url); //当不为空的时候直接解析 if (chaxun!=null) { Gson g=new Gson(); Jie jie = g.fromJson(chaxun, Jie.class); Jie.DataBean.WeatherBean weather = jie.getData().getWeather(); String city = jie.getData().getCity(); tv.setText(city+weather.getTomorrow_condition()+weather.getDat_low_temperature()+"---"+weather.getDat_high_temperature()+weather.getUpdate_time()+" 明天 "+weather.getTomorrow_weather_icon_id()+weather.getTomorrow_condition()+weather.getTomorrow_low_temperature()+"天气"+weather.getTomorrow_high_temperature()+"空气指数"+weather.getTomorrow_quality_level()); }else{ //否则请求数据 qingqiu(); }