"Android" Pa4d_ch6 using Internat Resources

Source: Internet
Author: User
Tags xml parser

  1. In Android, the basic way to use network resources is as follows (synchronous)
    Try{URL URL=NewURL (myfeed); //Create A new HTTP URL connectionURLConnection connection =url.openconnection (); HttpURLConnection httpconnection=(httpurlconnection) connection; intResponsecode =Httpconnection.getresponsecode (); if(Responsecode = =HTTPURLCONNECTION.HTTP_OK) {InputStream in=Httpconnection.getinputstream ();      Processstream (in); }    }    Catch(malformedurlexception e) {log.d (TAG,"Malformed URL Exception.", E); }    Catch(IOException e) {log.d (TAG,"IO Exception.", E); }
  2. At the same time, there are 3 main parsing XML in Android, including Dom parser, SAX parser and pull parser.
    1. Dom parser, Dombuilder, gets through documentbuilderfactory. These two classes are defined in the Javax package, and unlike J2SE, the latter has been rewritten in Android, directly acquiring the Apache Harmony implementation, and unfortunately, the Harmony project was abandoned by Apache in 2011.
       httpurlconnection httpconnection = int  responsecode = Httpconnection.getresponsecode ();  if  (Responsecode == HTTPCONNECTION.HTTP_OK) {InputStream in  = Httpconnection.getinputstream (); Documentbuilderfactory dbf  = Documentbuilderfactory. Newi    Nstance ();    Documentbuilder db  = Dbf.newdocumentbuilder ();     //  parse input     Document Dom = Db.parse (in); Element docele  = Dom.getdocumentelement ();}  
    2. SAX parser. Sax is a fast parsing and memory-intensive XML parser that is ideal for mobile devices such as Android. The Sax parsing XML file is event-driven, that is, it does not need to parse the entire document, and in the process of parsing the document in order of content, Sax determines whether the currently read character is a part of the valid XML syntax, and triggers the event if it conforms. The so-called events are actually some callback (callback) methods, which are defined in the ContentHandler interface.
      SAXParserFactory factory=saxparserfactory.newinstance ();  SAXParser parser=factory.newsaxparser ();  Myhandel Handel=new Myhandel ();  // here myhandle inherit from DefaulthandelParser.parse (InputStream, Handel);
    3. The
    4. pull parser. The following is the recommended use of this parser from Android Training,google
      Why should I learn the pull parser? Because pull parsing is looking for the desired tag in an XML document, pulling the required content into memory instead of pulling the entire document into memory, it is more suitable for small, mobile devices such as mobile phones with limited memory.

      We recommend Xmlpullparser , which is an efficient and maintainable a-to-parse XML on Android. Historically Android has had, implementations of this interface:

      • kxmlparser via Xm Lpullparserfactory.newpullparser () .
      • Expatpullparser , via xml.newpullparser () .
       factory =      Xmlpullparserfactory.newinstance (); Factory.setnamespaceaware ( true        = Factory.newpullparser (); 

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.