Android WebView not easy

Source: Internet
Author: User

Cell phone screen size very broken program ApeInspirational to become a javaproject division of me. Really hurt by it, not only because of the strong webview. And this content is suitable for all kinds of screen size problems.

I think Apple project teacher laughed at the work of the Android project teacher to deal with the adaptation of the screen, is simply in a repetitive job, did not expect many years later, Apple's mobile phone also appeared the screen size is not the same, Apple Project division crying in the toilet, everything really not absolutely ah!

1. Parsing an XML file for RSS Most of the news and magazines are now RSS, and we need to parse its XML file. But if you get the data inside, notice that it's not all the same, and you need to be careful.

 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); Instantiating a Template object while (Channel_iterator.hasnext ()) {//can only be placed in a map, not in the 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 writing, you need to write a part of the public method. Because is also in the study. All later will be encapsulated as a common method. 2. Formatting of the timebecause the article date format in RSS is: Thu, 2015 11:30:00 +0800.  Calendar Time format. The format we need is September 1, 2015.

        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          //Because local generates a very time-consuming, pre-generated        Locale local=locale.english;        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 ();}
   For this it should be used very often.3. Mobile phone size adaptationWebView is very powerful and can display HTML content, which allows the phone to parse HTML, and adds more functionality to the 5.0 webview. I need to show 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 to set the maximum size of the picture, but assume that the IMG tag is changed again, will make the picture of a lot of article loading becomes slow. So just to be able to compromise on this approach, (most of the methods written online still do not solve my needs.) Or on the Internet to find a solution to the day. The site does good, style I like the type, concise style) put all the Code show demo sample such as the following:
                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, I have finished writing the problems and solutions that I have encountered this week. Even though it was simply written down, it was a two-week study of Android. Started 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 ...





Android WebView not easy

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".

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.