学安卓复习java基础

因为项目需要刷新出昨天的文章,因此之前点击一份电子杂志,创建一个新的xml文件的方式就不对了,因为电子杂志不同于新闻,不会时时更新,因此一条更新一次,所以我需要每天下载一份它的RSS 源的xml文件到项目里(曾经居然想下载到手机的sd卡里),文件命名格式以时间加上杂志名,那么我每天需要创建这些xml,在项目下创建文件可以在files和cache中创建,我选择在files下创建。
                File file1 = null,file2=null,file3=null,file4=null;Date date2 = new Date();SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");//获取项目文件files的目录File tempFile = this.getFilesDir();String yygypathstr = tempFile.toString();//文件命名格式为以时间(年月日)+资源名file1 = new File(yygypathstr, (format2.format(date2)+"VICE中国"+ ".xml"));file2 = new File(yygypathstr, (format2.format(date2)+"设计癖"+ ".xml"));file3 = new File(yygypathstr, (format2.format(date2)+"知乎"+ ".xml"));file4 = new File(yygypathstr, (format2.format(date2)+"豆瓣一刻"+ ".xml"));//如果文件不存在,才创建if (!file1.exists() && !file2.exists() && !file3.exists() && !file4.exists()) {try {file1.createNewFile();file2.createNewFile();file3.createNewFile();file4.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

这个放在第一个activity中进行判断。创建好了之后,需要往这里面些数据,如果文件不为空且可以和RSS源连接上,就写入数据,我们需要往当天的那份杂志不为空的xml文件写入数据,用到循环判断。
                        Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");String paperTitleName=format.format(date)+title+ ".xml";File file=null;File fileXml=new File(xmlPath.toString());File[] tempList = fileXml.listFiles();//点击那一份杂志,找到这份杂志的当天xml赋给filefor (int i = 0; i < tempList.length; i++) {if (paperTitleName.equals(tempList[i].getName())) {file=tempList[i];}}
如果不为空就不存入数据
                        HttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(RSS_URL);try {HttpResponse response = client.execute(get);if (response.getStatusLine().getStatusCode() == 200 && file.length()==0) {InputStream inputStream = response.getEntity().getContent();FileOutputStream fos = new FileOutputStream(file);int byteread = 0;byte[] buffer = new byte[1024];while ((byteread = inputStream.read(buffer)) != -1) {fos.write(buffer, 0, byteread);}fos.flush();fos.close();inputStream.close();}} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
存入数据之后,开始解析xml文件读取数据,方法和我之前写的如何读取xml的方法一样。明天我将完成如何向下滑动,刷新出昨天的文章。



更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. 一句话锁定MySQL数据占用元凶
  3. Android(安卓)打开系统文件管理器,并返回选中文件的路径
  4. Android(安卓)完整地操作数据库--日记本实例
  5. Android(安卓)开启个人热点时 获取连接人数以及连接上的设备信息
  6. 【Android(安卓)Training - 06】分享数据内容 [Lesson 2 - 从其
  7. Android(安卓)数据持久化(SQLite数据存储)
  8. Android(安卓)图片下载本地内存的缓存方式
  9. 在Android中播放音频和视频

随机推荐

  1. Android(安卓)一些常用的但是记不住的设
  2. Android中的MD5加密
  3. android 实现无限定时循环viewpager
  4. Android(安卓)– Video/Music 视频音乐播
  5. android 倒影
  6. Android(安卓)自定义文本框(带图片)
  7. Android程序安装和卸载
  8. android 自动化测试 monkey
  9. android实现左右滑动菜单
  10. android 按钮的点击缩放