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;
}
}