Android WebView not easy

Source: Internet
Author: User
Tags date1 html header

Phone screen size is very hurt programmerinspirational to be a Java engineer, I was really hurt by it, not only because of the strong webview, but also this content for all kinds of screen size problems. I think the Apple engineers laughed at the Android engineers working overtime in the processing of the screen, is simply in the duplication of work, did not expect many years later, Apple's mobile phone also appeared the screen size is not the same, Apple engineers crying in the toilet, everything really does not absolutely ah! 1. Parsing the RSS XML filenow most of the news and magazines will give you RSS, we need to parse its XML file, but if you get the data inside, note that its label is not all the same, need attention!
 File tempfile = Environment.getexternalstoragedirectory (). Getabsolut Efile (); File File = new file (Tempfile, (Format.format (date) + ". xml")); HttpClient client = new Defaulthttpclient (); HttpGet get = new HttpGet (rss_url), try {httpresponse response = Client.execute (GET), if (Response.getstatusline (). Getstatuscode () = = () {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 ();} 
                        List=new arraylist<map<string,rssitem>> (); Saxreader reader = new Saxreader (); try {Document document = Reader.read (file);//The Document object element ro is obtained through the Read method of the reader object Otelement = Document.getrootelement (); Get root node element channelelement = Rootelement.element ("channel");//Get channel node iterator Channel_iterator = Channelelement.elementiterator ();            Prepare the first template that will need to change the time format of String PAT1 = "EEE, dd MMM yyyy HH:mm:ss Z";        Prepares the second template to change the extracted date number to the specified format String pat2 = "yyyy mm month dd date hh";        Locale Local=locale.english;        SimpleDateFormat sdf1 = new SimpleDateFormat (pat1,local);        Instantiate Template object SimpleDateFormat sdf2 = new SimpleDateFormat (PAT2); Instantiate template Object while (Channel_iterator.hasnext ()) {//can only be placed in map, cannot be sent to object Map<string,rssitem>map1=new hashmap< String,rssitem> (); Element itemelement = (Element) channel_iterator.next (); if ("Item". Equals (Itemelement.getname ())) {iterator item_ iterator = Itemelement.elemEntiterator (); rssitem = new RssItem (); while (Item_iterator.hasnext ()) {element itemchild = (Element) Item_iterator.next (); if ("title". Equals (Itemchild.getname ())) {Rssitem.settitle (Itemchild.getstringvalue ()); LOG.I ("RSS", Itemchild.getstringvalue ());} if ("link". Equals (Itemchild.getname ())) {Rssitem.setlink (Itemchild.getstringvalue ()); LOG.I ("RSS", Itemchild.getstringvalue ());} if ("description". Equals (Itemchild.getname ())) {rssitem.setdescription (Itemchild.getstringvalue ()); LOG.I ("RSS", Itemchild.getstringvalue ()); if (Getimgstr (Itemchild.getstringvalue ())!=null) {// The URL address of the first zone Rssitem.setimage (Getimgstr (Itemchild.getstringvalue ()));} Else{rssitem.setimage ("");}} if ("PubDate". Equals (Itemchild.getname ())) {Date date1= sdf1.parse (Itemchild.getstringvalue ()); String Pubdate=sdf2.format (date1); rssitem.setpubdate (pubDate); LOG.I ("RSS", PubDate);} if ("Author". Equals (Itemchild.getname ())) {Rssitem.setauthor (Itemchild.getstringvalue ()); LOG.I ("RSS", Itemchild.getstringvalue ());}} if (Rssitem.getimage ().Equals ("")) {}else {map1.put ("RssItem", RssItem); List.add (MAP1);} Feed.additem (MAP1);}}} catch (Documentexception e) {//Todo auto-generated catch Blocke.printstacktrace ()} catch (ParseException e) {//Todo Aut O-generated catch Blocke.printstacktrace ();} Message message = new Message (); Message.what = 1; Handler.sendmessage (message); Rss_url=null;}

Do not learn this way, you need to write a part of it as a public method. Because it is also learning, all later will be encapsulated into a common method. 2. Formatting of the timebecause the article date format in RSS is: Thu, 11:30:00 +0800, Calendar Time format, we need the format is September 1, 2015. How does this change? The internet is looking for a new DATE () as a conversion object, which is not in line with our actual situation, this time you need to query your JDK document, in the document can find his format: "EEE, dd MMM yyyy HH:mm:ss Z".
        String a= "Fri, 02:20:10 +0800";//string pat1 = "EEE, dd MMM yyyy HH:mm:ss Z";    EEE, dd MMM yyyy HH:mm:ss Z        //Prepare a second template that will extract the date number into the specified format            String pat2 = "yyyy years mm month DD Day HH mm min ss sec sss msec";  Moth          //The Locale Local=locale.english is generated in advance due to local generation time        ;        SimpleDateFormat sdf1 = new SimpleDateFormat (pat1,local);        Instantiate template Object            SimpleDateFormat sdf2 = new SimpleDateFormat (PAT2);        Instantiate Template Object            try {Date date= sdf1.parse (a); System.out.println (Sdf2.format (date));} catch (ParseException e) {e.printstacktrace ();}
   This should be very common for this.3. Mobile phone size adaptationWebView is powerful enough to display HTML content, which allows the phone to parse HTML, and adds more functionality to the 5.0 webview. I need to display the picture of the article, but the picture will be more than the screen size, in order to be able to display the effect in each machine, need to add these two lines of code:
            is the content with the mobile phone adapter    bodyhtml.getsettings (). Setusewideviewport (true);    Bodyhtml.getsettings (). Setloadwithoverviewmode (True);
This can really make the picture fit the size of the phone, but the font becomes smaller with the image, so we need to make the font larger:
            Sets the default font size    bodyhtml.getsettings (). Setdefaultfontsize (40);
Although this is not the best solution, should be in the content of the RSS article set the maximum size of the picture, but if the re-modification of the IMG tag, will make the picture of the article loading becomes slow, so only to compromise this approach, (most of the online writing method still does not solve my needs, Or in the bubble on the online day to find a solution to the site to do good, style I like the type, concise style) to show all the code as follows:
                Intent itent=getintent ();//article Content string Title=itent.getstringextra ("content");//Remove hyperlink, preserve font string regex= "<\\s*a.* ?/a\\s*> "; Title=title.replaceall (Regex," ");//Add HTML header string news=" 
Finally put this week encountered problems and solutions, although just write down, and still learned two weeks of Android, began to do the project, but really learned a lot of knowledge, especially the use of the knowledge of the Web, I also believe that WebApp is the future of the main tide!!!





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android WebView not easy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.