新闻的离线下载功能仿网易之(二) 带缓存的功能实现

来源:互联网 发布:手机淘宝如何实名认证 编辑:程序博客网 时间:2024/04/29 04:58

上次是单一频道的离线下载,这次要做更难一点儿的默认频道下载,包括后期的未加载页面的缓存和获取

接口如下



按照上面的接口解析时处理的比较复杂,相当于自己解析了两层之后再调用的gson解析,看代码

private DownNonet downNonetbean;private HttpInstance httpInstance;private List<DownDetail> downDetailList;//private List datamaps;private void HttpsDownLoad(final String serverURL) {new Thread(new Runnable() {public void run() {try {String data = httpInstance.getInstance().doHttpGet(serverURL);Log.d("zhsy", "serverURL==" + serverURL + "===down webview  data===" + data);JsonObject returnData = new JsonParser().parse(data).getAsJsonObject();JsonArray dataArr = returnData.getAsJsonArray("data");//     datamaps = new ArrayList();     downDetailList = new ArrayList<DownNonet.DownDetail>();for (int i  = 0;i<dataArr.size();i++) {Log.d("zhsy", "downDetailbean     0");JsonArray jsonElement = (JsonArray) dataArr.get(i);for (int j  = 0;j<jsonElement.size();j++) { JsonElement jsonElement2 = jsonElement.get(i); JsonObject asJsonObject = jsonElement2.getAsJsonObject();DownDetail downDetailbean = new Gson().fromJson(asJsonObject, DownDetail.class);downDetailList.add(downDetailbean);}//datamaps.add(downDetailList);//Log.d("zhsy", "downDetailbean     2"+downDetailList.get(0).name+"==="+datamaps.size());}if (!downDetailList.isEmpty()) {handler.sendEmptyMessage(110);} else if (downNonetbean.rt == 90001) {handler.obtainMessage(Constants.SEND_AUTCH_ERROR).sendToTarget();} else if (downNonetbean.rt == 91008) {handler.obtainMessage(Constants.SEND_TICKET_ERROR).sendToTarget();} else {handler.obtainMessage(Constants.SEND_FAILD).sendToTarget();}} catch (Exception e) {e.printStackTrace();handler.sendEmptyMessage(111);}}}).start();}

封装类

public class DownNonet implements Serializable {public int rt;public boolean success;public DownData data;public class DownData implements Serializable{public List<DownDetail> recommend;public List<DownDetail> shizheng;public List<DownDetail> junshi;public List<DownDetail> budui;public List<DownDetail> jujiao;public List<DownDetail> dianxing;public List<DownDetail> lilun;}public class DownDetail implements Serializable{public int id;public int cid;public String webview;public String name;public int home_cid;}}

按照cid存储到数据库缓存

private int beforePage = 0;private String tag = "category";private String type = "category";private String newsString;private NewsJson newDetail;private void HttpNewsDetail(final int cid) {//new Thread(new Runnable() {//public void run() {//try {beforePage = 1;String url = "";url = C.getNewsListAllUrl(cid, tag, type, beforePage);Log.d("dd", " url  cid==" + url);newsString = ssoController.getInstance().doHttpGet(url);newDetail = BeanUtils.newsBean(newsString);List<NewDetail> otherList1 = newDetail.data;Log.d("dd", "cid===" + cid);DBUtil.getInstance(activity).insertNewsData(otherList1, cid, beforePage, 1);//} catch (Exception e) {//e.printStackTrace();//handler.sendEmptyMessage(111);//}//}//}).start();}


正常新闻页面缓存时候的部分逻辑

Log.d("zhsy", " 有网络   265 channelDetail.cid = "+channelDetail.cid);List<NewDetail> noNetList = DBUtil.getInstance(getActivity()).getNewsData(channelDetail.cid + "");if (noNetList != null && noNetList.size() > 0 && page == 1) {if (newsArr != null) {newsArr.clear();newsArr.addAll(noNetList);}if (channelDetail.cid == Constants.CHANNEL_VIDEO) {handler.obtainMessage(Constants.CHANNEL_VIDEO).sendToTarget();} else if (channelDetail.cid == Constants.CHANNEL_SUBJECT) {handler.obtainMessage(Constants.CHANNEL_SUBJECT).sendToTarget();} else if (channelDetail.cid == Constants.CHANNEL_PIC) {handler.obtainMessage(Constants.CHANNEL_PIC).sendToTarget();} else {handler.obtainMessage(Constants.SEND_SUCCESS).sendToTarget();}} else {Log.d("zhsy", "zheli   1");newsString = "";beforePage = DBUtil.getInstance(activity).getPageNews(SQLHelper.TABLE_NEWS, channelDetail.cid + "") + 1;if (position == 0) {String url = "";String times = SPDataTools.spGetItemTime(activity,channelDetail.cid + "");if (TextUtils.isEmpty(times)) {url = C.getRecommandAllUrl(beforePage);} else {url = C.getRecommandUrl(times, beforePage);}newsString = ssoController.getInstance().doHttpGet(url);newDetail = BeanUtils.newsBean(newsString);handler.obtainMessage(Constants.NEWS_HAVE_DATA_TAG).sendToTarget();} else {String url = "";//正在刷新String times = SPDataTools.spGetItemTime(activity,channelDetail.cid + "");if (!TextUtils.isEmpty(times)) {url = C.getNewsListUrl(channelDetail.cid, channelDetail.tag, channelDetail.type, times,beforePage);} else {url = C.getNewsListAllUrl(channelDetail.cid, channelDetail.tag, channelDetail.type, beforePage);}Log.d(TAG, " url  " + url );newsString = ssoController.getInstance().doHttpGet(url);newDetail = BeanUtils.newsBean(newsString);handler.obtainMessage(Constants.NEWS_HAVE_DATA_TWO_TAG).sendToTarget();}}


数据库的逻辑

public static NewsJson newsBean(String jsonStirng){Gson gson = new Gson();NewsJson newsJson = new NewsJson();try {newsJson = gson.fromJson(jsonStirng, NewsJson.class);} catch (JsonSyntaxException e) {// TODO Auto-generated catch blocke.printStackTrace();}return newsJson;}


private static DBUtil mInstance;private Context mContext;private SQLHelper mSQLHelp;private SQLiteDatabase mSQLiteDatabase;private DBUtil(Context context) {mContext = context;mSQLHelp = new SQLHelper(context);if (mSQLHelp != null) {mSQLiteDatabase = mSQLHelp.getWritableDatabase();}}/** * 初始化数据库操作DBUtil类 */public static DBUtil getInstance(Context context) {if (mInstance == null) {mInstance = new DBUtil(context);}return mInstance;}/** * 关闭数据库 */public void close() {if (mInstance != null) {mInstance = null;}if (mSQLHelp != null) {mSQLHelp.close();mSQLHelp = null;}if (mSQLiteDatabase != null) {mSQLiteDatabase.close();mSQLiteDatabase = null;}}public void insertNewsData(List<NewDetail> list, int cid, int page, int mark) {if (mSQLiteDatabase == null) {mSQLiteDatabase = mSQLHelp.getWritableDatabase();}try {//int size = getNewsCount(SQLHelper.TABLE_NEWS, cid + "");//if (size <= 60 && mark == 1) {for (int i = 0; i < list.size(); i++) {NewDetail detail = list.get(i);ContentValues values = new ContentValues();SaveNetail(values, detail);values.put(SQLHelper.NEWS_CID, cid);// 判断是新闻的那个分类values.put(SQLHelper.PAGE, page);// 判断是新闻的那个分类values.put(SQLHelper.MARK, mark);// 判断是上拉还是下拉if (!isExit(SQLHelper.TABLE_NEWS, detail.id, detail.cid, cid + "")) {mSQLiteDatabase.insert(SQLHelper.TABLE_NEWS, null, values);}}//} else if (mark == 0) {//for (int i = 0; i < list.size(); i++) {//NewDetail detail = list.get(i);//ContentValues values = new ContentValues();//SaveNetail(values, detail);//values.put(SQLHelper.NEWS_CID, cid);// 判断是新闻的那个分类//values.put(SQLHelper.PAGE, page);// 判断是新闻的那个分类//values.put(SQLHelper.MARK, mark);// 判断是上拉还是下拉//if (!isExit(SQLHelper.TABLE_NEWS, detail.id, detail.cid, cid + "")) {//mSQLiteDatabase.insert(SQLHelper.TABLE_NEWS, null, values);//}//}//}} catch (Exception e) {Log.d("lijia", "insert  eee=" + e);}}//private int getNewsCount(String table, String cid) {//int x = 0;//String sql = "select count(*) from " + table + " where " + SQLHelper.NEWS_CID + "=?";//Cursor cursor = mSQLiteDatabase.rawQuery(sql, new String[] { cid + "" });//if (cursor.moveToFirst()) {//x = cursor.getInt(0);//}//return x;//}/** * @param conntext * @param cid * @param table */public List<NewDetail> getNewsData(String cid) {if (mSQLiteDatabase == null) {mSQLiteDatabase = mSQLHelp.getWritableDatabase();}List<NewDetail> mList = new ArrayList<NewDetail>();// 先从mark=0 获取,在从mark=1 获取Cursor cursor1 = null;Cursor cursor2 = null;String sql = "select * from " + SQLHelper.TABLE_NEWS + " where " + SQLHelper.NEWS_CID + " =? AND "+ SQLHelper.MARK + " =? " + "order by _id ASC ";try {cursor1 = mSQLiteDatabase.rawQuery(sql, new String[] { cid + "", "0" });while (cursor1.moveToNext()) {mList.add(getNetail(cursor1));}cursor2 = mSQLiteDatabase.rawQuery(sql, new String[] { cid + "", "1" });while (cursor2.moveToNext()) {mList.add(getNetail(cursor2));}} catch (Exception e) {Log.d("lijia", "get news data  ee" + e);} finally {if (cursor1 != null) {cursor1.close();}if (cursor2 != null) {cursor2.close();}}return mList;}private NewDetail getNetail(Cursor cursor) {NewDetail detail = new NewDetail();detail.addtime = cursor.getString(cursor.getColumnIndex(SQLHelper.ADDTIME));detail.id = cursor.getInt(cursor.getColumnIndex(SQLHelper.ID));detail.cid = cursor.getInt(cursor.getColumnIndex(SQLHelper.CID));detail.commentnum = cursor.getInt(cursor.getColumnIndex(SQLHelper.COMMENTNUM));detail.ishot = cursor.getInt(cursor.getColumnIndex(SQLHelper.ISHOT));detail.istop = cursor.getInt(cursor.getColumnIndex(SQLHelper.ISTOP));detail.pic_number = cursor.getInt(cursor.getColumnIndex(SQLHelper.PIC_NUMBER));detail.publisher_hid = cursor.getString(cursor.getColumnIndex(SQLHelper.PUBLISHER_HID));detail.publisher_name = cursor.getString(cursor.getColumnIndex(SQLHelper.PUBLISHER_NAME));detail.publisher_photo = cursor.getString(cursor.getColumnIndex(SQLHelper.PUBLISHER_PHOTO));detail.webview = cursor.getString(cursor.getColumnIndex(SQLHelper.WEBVIEW));detail.title = cursor.getString(cursor.getColumnIndex(SQLHelper.TITLE));detail.thumb = cursor.getString(cursor.getColumnIndex(SQLHelper.THUMB));detail.type = cursor.getString(cursor.getColumnIndex(SQLHelper.TYPE));String imageStr = cursor.getString(cursor.getColumnIndex(SQLHelper.NEWS_IMAGURE));if (TextUtils.isEmpty(imageStr)) {detail.news_imgurl = null;} else {try {String[] strings = imageStr.split("#");List<NewDetail.ImageUrlItem> list = new ArrayList<NewDetail.ImageUrlItem>();if (strings != null && strings.length > 0) {for (int i = 0; i < strings.length; i++) {String[] aaa = strings[i].split(",");NewDetail.ImageUrlItem image = new NewDetail.ImageUrlItem();image.url = aaa[0];image.alt = aaa[1];list.add(image);}detail.news_imgurl = list;} else {detail.news_imgurl = null;}} catch (Exception e) {}}return detail;}private void SaveNetail(ContentValues values, NewDetail detail) {values.put(SQLHelper.ADDTIME, detail.addtime);values.put(SQLHelper.ID, detail.id);values.put(SQLHelper.CID, detail.cid);values.put(SQLHelper.COMMENTNUM, detail.commentnum);values.put(SQLHelper.ISHOT, detail.ishot);values.put(SQLHelper.ISTOP, detail.istop);values.put(SQLHelper.PIC_NUMBER, detail.pic_number);values.put(SQLHelper.PUBLISHER_HID, detail.publisher_hid);values.put(SQLHelper.PUBLISHER_NAME, detail.publisher_name);values.put(SQLHelper.PUBLISHER_PHOTO, detail.publisher_photo);values.put(SQLHelper.WEBVIEW, detail.webview);values.put(SQLHelper.TITLE, detail.title);values.put(SQLHelper.THUMB, detail.thumb);values.put(SQLHelper.TYPE, detail.type);if (detail.news_imgurl != null && detail.news_imgurl.size() > 0) {String urlstring = "";for (int s = 0; s < detail.news_imgurl.size(); s++) {urlstring += detail.news_imgurl.get(s).toString() + "#";}values.put(SQLHelper.NEWS_IMAGURE, urlstring);} else {values.put(SQLHelper.NEWS_IMAGURE, "");}}public void insertSubjectData(List<NewDetail> list, int page, int cid, int mark) {if (mSQLiteDatabase == null) {mSQLiteDatabase = mSQLHelp.getWritableDatabase();}try {//int size = getNewsCount(SQLHelper.TABLE_SUBJECT, cid + "");//if (size <= 60 && mark == 1) {for (int i = 0; i < list.size(); i++) {NewDetail detail = list.get(i);ContentValues values = new ContentValues();SaveNetail(values, detail);//values.put(SQLHelper.NEWS_CID, cid);// 判断是新闻的那个分类values.put(SQLHelper.PAGE, page);values.put(SQLHelper.MARK, mark);// 判断上拉还是下拉if (!isExit(detail.id, detail.cid, SQLHelper.TABLE_SUBJECT)) {mSQLiteDatabase.insert(SQLHelper.TABLE_SUBJECT, null, values);}}//} else if (mark == 0) {//for (int i = 0; i < list.size(); i++) {//NewDetail detail = list.get(i);//ContentValues values = new ContentValues();//SaveNetail(values, detail);//values.put(SQLHelper.NEWS_CID, cid);// 判断是新闻的那个分类//values.put(SQLHelper.PAGE, page);//values.put(SQLHelper.MARK, mark);// 判断上拉还是下拉//if (!isExit(detail.id, detail.cid, SQLHelper.TABLE_SUBJECT)) {//mSQLiteDatabase.insert(SQLHelper.TABLE_SUBJECT, null, values);//}//}//}} catch (Exception e) {Log.d("lijia", "insert subject eee=" + e);}}/** * @param conntext * @param cid */public List<NewDetail> getSubjectData(String cid) {if (mSQLiteDatabase == null) {mSQLiteDatabase = mSQLHelp.getWritableDatabase();}List<NewDetail> mList = new ArrayList<NewDetail>();Cursor cursor1 = null;Cursor cursor2 = null;String sql = "select * from " + SQLHelper.TABLE_SUBJECT + " where " + SQLHelper.CID + " =? AND "+ SQLHelper.MARK + " =? " + "order by _id asc ";try {Log.d("lijia", " get subject  sql==" + sql);cursor1 = mSQLiteDatabase.rawQuery(sql, new String[] { cid + "", "0" });while (cursor1.moveToNext()) {mList.add(getNetail(cursor1));}cursor2 = mSQLiteDatabase.rawQuery(sql, new String[] { cid + "", "1" });while (cursor2.moveToNext()) {mList.add(getNetail(cursor2));}} catch (Exception e) {Log.d("lijia", "get subject  data  ee" + e);return null;} finally {if (cursor1 != null) {cursor1.close();}if (cursor2 != null) {cursor2.close();}}return mList;}public void insertSubjectDetailData(List<NewDetail> list, String scid, int page, int mark) {if (mSQLiteDatabase == null) {mSQLiteDatabase = mSQLHelp.getWritableDatabase();}try {//int size = getNewsCount(SQLHelper.TABLE_SUBJECT_DETAIL, scid + "");//if (size <= 60 && mark == 1) {for (int i = 0; i < list.size(); i++) {NewDetail detail = list.get(i);ContentValues values = new ContentValues();SaveNetail(values, detail);values.put(SQLHelper.PAGE, page);// 判断是新闻的那个分类values.put(SQLHelper.MARK, mark);// 判断是新闻的那个分类values.put(SQLHelper.NEWS_CID, scid);if (!isExit(SQLHelper.TABLE_SUBJECT_DETAIL, detail.id, detail.cid, scid)) {mSQLiteDatabase.insert(SQLHelper.TABLE_SUBJECT_DETAIL, null, values);}}//} else if (mark == 0) {//for (int i = 0; i < list.size(); i++) {//NewDetail detail = list.get(i);//ContentValues values = new ContentValues();//SaveNetail(values, detail);//values.put(SQLHelper.PAGE, page);// 判断是新闻的那个分类//values.put(SQLHelper.MARK, mark);// 判断是新闻的那个分类//values.put(SQLHelper.NEWS_CID, scid);//if (!isExit(SQLHelper.TABLE_SUBJECT_DETAIL, detail.id, detail.cid, scid)) {//mSQLiteDatabase.insert(SQLHelper.TABLE_SUBJECT_DETAIL, null, values);//}//}//}} catch (Exception e) {Log.d("lijia", "insert  eee=" + e);}}/** * @param conntext * @param cid * @param table *            判断上拉 还是下拉 */public List<NewDetail> getSubjectDetialData(String scid) {if (mSQLiteDatabase == null) {mSQLiteDatabase = mSQLHelp.getWritableDatabase();}List<NewDetail> mList = new ArrayList<NewDetail>();Cursor cursor1 = null;Cursor cursor2 = null;String sql = "select * from " + SQLHelper.TABLE_SUBJECT_DETAIL + " where " + SQLHelper.NEWS_CID + " =? AND "+ SQLHelper.MARK + " =? order by _id asc ";try {cursor1 = mSQLiteDatabase.rawQuery(sql, new String[] { scid, "0" });while (cursor1.moveToNext()) {mList.add(getNetail(cursor1));}cursor2 = mSQLiteDatabase.rawQuery(sql, new String[] { scid, "1" });while (cursor2.moveToNext()) {mList.add(getNetail(cursor2));}} catch (Exception e) {Log.d("lijia", "get subject detail  ee" + e);return null;} finally {if (cursor1 != null) {cursor1.close();}if (cursor2 != null) {cursor2.close();}}return mList;}



阅读全文
0 0