Learn Android Review Java basicsBecause the project needs to refresh yesterday's article, so before clicking on an E-magazine, the way to create a new XML file is not right, because the E-magazine is different from the news, not always updated, so one update, so I need to download a copy of its RSS every day The source XML file into the project (once actually wanted to download the SD card to the phone), file naming format with time Plus magazine name, then I need to create these XML every day, the creation of files under the project can be created in files and cache, I choose to create under files.
File file1 = null,file2=null,file3=null,file4=null;date date2 = new Date (); SimpleDateFormat format2 = new SimpleDateFormat ("YyyyMMdd");//Get the project files directory file Tempfile = This.getfilesdir (); String yygypathstr = tempfile.tostring ();//file naming format with time (month day) + resource Name file1 = new File (Yygypathstr, (Format2.format (date2) + " Vice China "+". xml "), File2 = new File (Yygypathstr, (Format2.format (date2) +" designer "+". xml ")); file3 = new File (Yygypathstr, ( Format2.format (Date2) + "know" + ". xml"), File4 = new File (Yygypathstr, (Format2.format (date2) + "watercress moment" + ". xml"));//If the file does not exist To create 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 ();}}
This is judged by the first activity. After the creation, we need to face some data, if the file is not empty and can be connected to the RSS feed, write data, we need to the same day that the magazine is not empty XML file to write data, using the loop to judge.
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 ();//click on that magazine to find the day of the magazine XML assigned to filefor (int i = 0; i < templist.length; i++) {if (Papert Itlename.equals (Templist[i].getname ())) {file=templist[i];}}
If not empty, do not deposit data
HttpClient client = new Defaulthttpclient (); HttpGet get = new HttpGet (rss_url), try {httpresponse response = Client.execute (GET), if (Response.getstatusline (). Getstatuscode () = = && File.length () ==0) {InputStream InputStream = response.getentity (). getcontent (); FileOutputStream fos = new FileOutputStream (file), int byteread = 0;byte[] buffer = new Byte[1024];while ((Byteread = input Stream.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 ();}
After the data has been deposited, the parsing XML file is read in the same way as I wrote before to read the XML. Tomorrow I will finish how to swipe down and refresh out of yesterday's article.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android projects create XML and store XML files