afnetworking 2.0 XML Request processing Topics1.1 XML processing mechanism of 1.1.1 XML request processing
Because in AFNetworking2.0, the XML format returned by the response is not specifically parsed, so developers need to handle it themselves. In the author's project, a third-party class library called Afgdataxmlrequestoperation is used for unified processing.
The XML parsing logic of the library is based on the Gdataxml-html class library. However, the class library processing logic should still be based on the Afnetworking 1.0 version, there are two needs to make adaptation changes, otherwise debugging:
1, in the Responsexmldocument method, the Responsexmldocument initialization method is adjusted;
2, in the Setcompletionblockwithsuccess method, will fail the queue Self.failurecallbackqueue and success Queueself.successcallbackqueue Unified Change to Self.completionqueue
Afgdataxmlrequestoperation:
Https://github.com/graetzer/AFGDataXMLRequestOperation
Gdataxml-html Library:
Https://github.com/graetzer/GDataXML-HTML
1.1.2 Sample Code
nsurlrequest * request = [[nsurlrequestalloc]initwithurl: [[Nsurl Alloc]initwithstring:@ "http://rss.sina.com.cn/tech/index.shtml"];
afgdataxmlrequestoperation * Xmlopera = [afgdataxmlrequestoperation Xmldocumentrequestoperationwithrequest: RequestSuccess: ^ (nsurlrequest * request, Nshttpurlresponse * response,gdataxmldocument * xmldoc) {
gdataxmlelement * elem = [[[XmlDocrootelement]elementsforname:@ "REC"] Objectatindex:0];
NSLog (@ "Xmldocumentfirst Node name:%@", [Elemattributeforname:@ "name"]);
} Failure: ^ (nsurlrequest *request,nshttpurlresponse * response,nserror * error, gdataxmldocument * xmldoc) {
NSLog (@ "Requestflagshipstoredataerror:%@", error);
}];
[Xmlopera start];
1.1.3 Method Two--handle XML parsing yourself
Because the Responseobject returned in the afnetworking callback is a Nsxmlparser class for starting and parsing XML, we can also implement the nsxmlparserdelegate interface Ourselves, To implement XML parsing yourself.
1.1.4 Custom XML Parsing sample code 1.1.4.1 XML Request processing logic
afhttprequestoperationmanager *manager = [afhttprequestoperationmanagermanager];
Afxmlparserresponseserializer * responseser = [afxmlparserresponseserializernew];
[Manager Setresponseserializer: Responseser];
nsdictionary *dict =@{@ "format":@ "xml"};
[Manager GET:@ "http://rss.sina.com.cn/tech/index.shtml" parameters:d ICT Success: ^ (afhttprequestoperation *operation,ID responseobject) {
nsxmlparser * parser = (nsxmlparser *) Responseobject;
Parser. delegate =self;
[Parser setshouldprocessnamespaces:YES];
// This method is basically feasible, but dealing with trouble
[Parser parse];
Logdebug (@ "Requestflagshipstoredata xml:%@", parser);
} Failure: ^ (afhttprequestoperation *operation,nserror *error) {
Logdebug (@ "Requestflagshipstoredata error:%@", Error);
}];
1.1.4.2 nsxmlparserdelegate Delegate Processing
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////
#pragma mark-nsxmlparserdelegate Delegate Processing
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////
// triggered at the beginning of the document
-(void) Parserdidstartdocument: (nsxmlparser *) parser{
Logdebug (@ " parsing begins! ");
}
// Parse start tag
-(void) Parser: (nsxmlparser *) Parser didstartelement: (nsstring *) Elementnamenamespaceuri: (nsstring *) Namespaceuriqualifiedname: (nsstring *) QName attributes: ( nsdictionary *) attributedict{
Logdebug (@ " mark:%@", elementname);
}
// parsing text Nodes
-(void) Parser: (nsxmlparser *) Parser foundcharacters: (nsstring *) string{
Logdebug (@ " value:%@", String);
}
// Parse end tag
-(void) Parser: (nsxmlparser *) Parser didendelement: (nsstring *) Elementnamenamespaceuri :(nsstring *) Namespaceuriqualifiedname: (nsstring *) qname{
Logdebug (@ " end tag:%@", elementname);
}
// trigger at end of document
-(void) Parserdidenddocument: (nsxmlparser *) parser{
Logdebug (@ " parsing is over! ");
}
The XML request processing feature in IOS afnetworking 2.0