Department of famous Door Android (10)

Source: Internet
Author: User
Tags closing tag stub

Introduced

HTTP communication with server in Android, parsing XML, implementing asynchronous message processing via Handler

HTTP communication-HTTP communication with the server, to be demonstrated in Get and POST mode, respectively

XML parsing-XML can be parsed in two ways, DOM and SAX, respectively

Asynchronous message processing-asynchronous message processing via Handler, using a custom asynchronous download class to illustrate the use of Handler

1, HTTP Communication and XML parsing Demo

Mysaxhandler.java

Package com.webabcd.communication;

Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;

Inherits DefaultHandler to implement the SAX parser for the specified XML
DOM-W3C Standard, the XML data needs to be fully loaded before it can be parsed, can be arbitrary traversal of the tree
SAX-streaming parsing, parsing XML through the event model, only parsing in sequence
public class Mysaxhandler extends DefaultHandler {

Private Boolean mistitletag=false;
Private Boolean missalarytag=false;
Private Boolean misbirthtag=false;
Private String mresult= "";

To open a callback function for an XML document
@Override
public void Startdocument () throws Saxexception {
TODO auto-generated Method Stub
Super.startdocument ();
}

To close the callback function for an XML document
@Override
public void Enddocument () throws Saxexception {
TODO auto-generated Method Stub
Super.enddocument ();
}

Callback this function as soon as an element start tag is found
@Override
public void Startelement (string uri, String localname, String qName,
Attributes Attributes) throws Saxexception {
if (LocalName = = "title")
Mistitletag=true;
else if (LocalName = "salary")
Missalarytag=true;
else if (LocalName = "dateOfBirth")
Misbirthtag=true;
else if (LocalName = "Employee")
Mresult + = "\nname:" + attributes.getvalue ("name");
}

Callback this function as soon as an element closing tag is found
@Override
public void EndElement (string uri, String localname, String qName)
Throws Saxexception {
if (LocalName = = "title")
Mistitletag=false;
else if (LocalName = "salary")
Missalarytag=false;
else if (LocalName = "dateOfBirth")
Misbirthtag=false;
}

Callback this function when an element value or attribute value is found
@Override
public void characters (char[] ch, int start, int length)
Throws Saxexception {
if (Mistitletag)
Mresult + + new String (CH, start, length);
else if (Missalarytag)
Mresult + = "Salary:" + New String (CH, start, length);
else if (Misbirthtag)
Mresult + = "dateOfBirth:" + New String (CH, start, length);
}

Public String GetResult () {
return mresult;
}
}

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.