Using the third-party library afnetworking to parse the JSON and MXL data requested by the network has many convenient places.
The third party libraries: https://github.com/AFNetworking/AFNetworking
Imported package and Macro definitions
1 #import "ViewController.h"2 #import "AFNetworking.h"3 #import "GDataXMLNode.h"4 5 //JSON address6 #definekjsonurlstring @ "http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1 "7 8 //the address of the XML9 #definekxmlurlstring @ "http://wiapi.hexun.com/news/getlist4.0.php?pid=100234721&pc=20&pn=1&st=0 "Ten One //JSON post submission method A #definekjsonposturlstring @ "http://Api2.dfc.cn/sjll/v1/homes/basic_data " - - //XML Post Submission method the #definekxmlposturlstring @ ""
1. How to request JSON get mode
1 //How to request a JSON get method2- (void) Jsongetrequest3 {4Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager manager];5Manager.responseSerializer.acceptableContentTypes = [Nsset setwithobject:@"Application/json"];6[Manager get:kjsonurlstring Parameters:nil success:^ (afhttprequestoperation *operation,IDresponseobject) {7 if([Responseobject Iskindofclass:[nsdataclass]]) {8NSLog (@"Responseobjectdata =%@", responseobject);9 }Ten Else if([Responseobject Iskindofclass:[nsdictionaryclass]]) One { ANSLog (@"responseobjectdict =%@", responseobject); - } -} failure:^ (afhttprequestoperation *operation,nserror *error) { the - }]; - //get how many requests are in a queue -Nsinteger count =Manager.operationQueue.operationCount; +NSLog (@"count =%ld", count); - //Cancel all requests in the queue + [Manager.operationqueue cancelalloperations]; A}
2. How to request XML in the Get mode
It also uses a third-party library provided by Google Gdata to parse the XML file, github:https://github.com/graetzer/gdataxml-html
//Error: 3840---means that the data for the server segment is not strictly written in XML format//XML parsing- (void) xmlgetrequest{Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; //manager.responseSerializer.acceptableContentTypes = [Nsset setwithobject:@ "Text/xml"]; //generate a serialized object of your own response, without using the system-given wayManager.responseserializer =[[Afhttpresponseserializer alloc] init]; [Manager get:kxmlurlstring Parameters:nil success:^ (Afhttprequestoperation *operation,IDresponseobject) {gdataxmldocument*doc=[[Gdataxmldocument alloc] Initwithdata:responseobject encoding:nsutf8stringencoding Error:nil]; NSString*path =@"/doc/focus/frame/title"; Nsarray*titles=[Doc Nodesforxpath:path Error:nil]; for(Gdataxmlelement *titleinchtitles) {NSLog (@"%@", Title.stringvalue); } //NSLog (@ "requestobject =%@", responseobject);} failure:^ (Afhttprequestoperation *operation,nserror *error) {NSLog (@"error =%@", error); }];}
3. How to request a JSON post
//POST request data operation JSON data- (void) Jsonpostrequest: (Nsdictionary *) dict{//single-instance objectsAfhttprequestoperationmanager *manager =[Afhttprequestoperationmanager Manager]; Manager.responseSerializer.acceptableContentTypes= [Nsset setwithobject:@"Application/json"]; [Manager post:kjsonposturlstring parameters:dict success:^ (Afhttprequestoperation *operation,IDresponseobject) {NSLog (@"Responseobject =%@", Responseobject); } Failure:^ (Afhttprequestoperation *operation,nserror *error) {NSLog (@"error =%@", error); }];}
4. How to post the XML request
1 //Post method request XML data2- (void) Xmlpostrequest: (Nsdictionary *) Dict3 {4Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager manager];5 //manager.responseSerializer.acceptableContentTypes = [Nsset setwithobject:@ "Text/xml"];6 //parse XML using the object you created7Manager.responseserializer =[Afhttpresponseserializer alloc];8[Manager post:kxmlposturlstring Parameters:dict success:^ (afhttprequestoperation *operation,IDresponseobject) {9 TenNSLog (@"Responseobject =%@", responseobject); One A} failure:^ (afhttprequestoperation *operation,nserror *error) { - -NSLog (@"error =%@", error); the - }]; -}
iOS development-parse JSON and XML data using third-party library afnetworking