网络请求的数据,添加到数据库,断开网络直接查询

1.创建数据库
public class MHelp extends SQLiteOpenHelper {

//创建数据库
public MHelp(Context context) {
    super(context, "jh.db", null, 1);
}

//创建数据表
@Override
public void onCreate(SQLiteDatabase db) {
    //db.execSQL("create table jh_user (id Integer primary key autoincrement ,  date text,thumbnail_pic_s text,thumbnail_pic_s02 text,thumbnail_pic_s03 text ,title text)");
    db.execSQL("create table jh_user (id Integer primary key autoincrement , author_name text , date text,thumbnail_pic_s text,thumbnail_pic_s02 text,thumbnail_pic_s03 text ,title text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}
2.创建Dao
public class UserDao{

private final SQLiteDatabase database;

public UserDao(Context context) {
    //创建数据库
    MHelp mHelp = new MHelp(context);
    database = mHelp.getReadableDatabase();
}

//添加
public void insert(String author_name, String date, String thumbnail_pic_s, String thumbnail_pic_s02, String thumbnail_pic_s03, String title) {
    database.execSQL("insert into jh_user (author_name,date,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03, title) values(?,?,?,?,?,?)",new Object[]{author_name,date,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03,title});
}



//查询
public List<ResponBean.DataItem> query() {
    Cursor cursor = database.rawQuery("select * from jh_user", null);
    List<ResponBean.DataItem> list = new ArrayList<>();
    while (cursor.moveToNext()){
        String author_name = cursor.getString(cursor.getColumnIndex("author_name"));
        String date = cursor.getString(cursor.getColumnIndex("date"));
        String thumbnail_pic_s = cursor.getString(cursor.getColumnIndex("thumbnail_pic_s"));
        String thumbnail_pic_s02 = cursor.getString(cursor.getColumnIndex("thumbnail_pic_s02"));
        String thumbnail_pic_s03 = cursor.getString(cursor.getColumnIndex("thumbnail_pic_s03"));
        String title = cursor.getString(cursor.getColumnIndex("title"));
        ResponBean.DataItem dataItem = new ResponBean.DataItem(author_name, date, thumbnail_pic_s, thumbnail_pic_s02, thumbnail_pic_s03, title);
        list.add(dataItem);
    }
    return list;
}

}
//这是一个接口中的方法
@Override
public void doString(List<ResponBean.DataItem> dataItems) {
if(dataItems == null){
Toast.makeText(getActivity(), “数据请求错误”, Toast.LENGTH_SHORT).show();
return;
}
updateData(dataItems);
loadComplete();

    //向数据库添加数据
    for(int i=0 ; i<dataItems.size();i++){
        String author_name = dataItems.get(i).getAuthor_name();
        String date = dataItems.get(i).getDate();
        String thumbnail_pic_s = dataItems.get(i).getThumbnail_pic_s();
        String thumbnail_pic_s02 = dataItems.get(i).getThumbnail_pic_s02();
        String thumbnail_pic_s03 = dataItems.get(i).getThumbnail_pic_s03();
        String title = dataItems.get(i).getTitle();
        dao.insert(author_name,date,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03,title);
    }

}
3.查询数据库并展示
//判断是否有网络,一般写在程序的开头
    if(!NetWork.isNetwork(getActivity())){
        Toast.makeText(getActivity(), "网络不可用", Toast.LENGTH_SHORT).show();
        //跳转设置界面
        //startActivity(new Intent(Settings.ACTION_SETTINGS));
        //创建一个线程查询数据库并赋值
        thread();
    }else{

    }
    return view;
}

//查询数据库并展示
private void thread() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            List<ResponBean.DataItem> query = dao.query();
            adapter.setData(query);
        }
    }){}.start();
}
4.用到的工具类
public class NetWork {
/**
 * 判断是否有网络
 * */
public static boolean isNetwork(Context context){
    //获取系统服务,连接服务
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
   //获取活动的网络信息
    NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
    //可用网络不为空且可用的话,说明有网
    return activeNetworkInfo != null && activeNetworkInfo.isAvailable();
}

}

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:书香水墨 设计师:CSDN官方博客 返回首页
评论

打赏作者

爱笑re

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值