In the previous article, we added the kissxml, mbprogresshud, and aqridview libraries to our client. First, we added the kissxml library, which is an XML parsing library and supports XPath, so that we can easily add and modify any node. Download the kissxml library from the official website.
Https://github.com/robbiehanson/KissXML
Next, import the kissxml folder in the kissxml library to our project,
Then, we added the file/Add file to "tuan1" option to our project, and added the mbprogresshud and agridview libraries in the same way.
Add quartzcore. Framework to the framework because dview is required.
Then we add
# Import "mbprogresshud. H"
# Import "ddxml. H"
Run these two lines. If no error is reported, the databases will be successfully added.
Next, let's change storybord, add the firstviewcontroller view to navigationcontroller, select firstviewcontroller in the main menu Editor/embed in/navigation controller, and the system automatically adds navigationcontroller to us.
Next, we will clear the firstviewcontroller and delete all contents.
Okay, at this step, our views are basically complete, followed by encoding. First, let's parse group buying information of Meituan.
Firstviewcontroller. h
#import <UIKit/UIKit.h>#import "ASIHTTPRequest.h"@interface KKFirstViewController : UIViewController<ASIHTTPRequestDelegate>@end
Firstviewcontroller. m
# Import "kkfirstviewcontroller. H "# import" mbprogresshud. H "# import" ddxml. H "# import" ddxmlelementadditions. H "@ interface kkfirstviewcontroller () {mbprogresshud * HUD; nsmutablearray * arrays;} @ end @ implementation kkfirstviewcontroller-(void) viewdidload {[Super viewdidload]; self. navigationitem. title = @ "Meituan"; // background uiimage * bgimage = [uiimage imagenamed: @ "bg-app.png"]; self. view. backgroundcolor = [uico Lor colorwithpatternimage: bgimage]; // HUD prompt box HUD = [[mbprogresshud alloc] init]; HUD. labeltext = @ "load data... "; // website nsstring * urlstring = @" http://www.meituan.com/api/v2/beijing/deals "; [HUD showwhileexecuting: @ selector (starthttprequest :) ontarget: Self withobject: urlstring animated: Yes]; // because the HUD needs to be displayed at the beginning, we add [self. navigationcontroller. view addsubview: HUD]; arrays = [nsmutable Array array];} // request URL-(void) starthttprequest :( nsstring *) URL {asihttprequest * httprequest = [[asihttprequest alloc] initwithurl: [nsurl urlwithstring: url]; httprequest. delegate = self; [httprequest startsynchronous];}-(void) viewdidunload {[Super viewdidunload]; // release any retained subviews of the main view .} -(bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorien Tation {return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown);} // request end-(void) requestfinished :( asihttprequest *) Request {// remove the prompt box [HUD removefromsuperview]; // return the parsed data, each array includes a dictionary arrays = [self xmlparser: [Request responsedata]; nslog (@ "% @", arrays );} // The parsing node static nsstring * kxpath_item = @ "// data"; static nsstring * kname_deal = @ "deal"; static nsstring * kname_title = @ "deal_title "; static nsstring * kname_url = @ "deal_url"; static nsstring * kname_img = @ "Hangzhou"; static nsstring * kname_desc = @ "deal_desc"; static nsstring * kname_tips = @ "deal_tips "; // XML parsing-(nsmutablearray *) xmlparser :( nsdata *) Data {nsmutablearray * array = [nsmutablearray array]; // The document starts ddxmldocument * xmldoc = [[ddxmldocument alloc] initwithdata: data options: 0 error: Nil]; // return all elements in <DATA> nsarray * items = [xmldoc nodesforxpath: kxpath_item error: Nil]; // traverse each element for (ddxmlelement * item in items) {nsmutabledictionary * dict = [nsmutabledictionary dictionary]; // return all elements in <deal> nsarray * deals = [item elementsforname: kname_deal]; for (ddxmlelement * deal in deals) {// <deal_title> ddxmlelement * Title = [deal elementforname: kname_title]; If (title) {[dict setobject: [Title stringvalue] forkey: kname_title];} // <deal_url> ddxmlelement * url = [deal elementforname: kname_url]; If (URL) {[dict setobject: [url stringvalue] forkey: kname_url] ;}// <deal_img> ddxmlelement * IMG = [deal elementforname: kname_img]; If (IMG) {[dict setobject: [img stringvalue] forkey: kname_img];} // <deal_desc> ddxmlelement * DESC = [deal elementforname: kname_desc]; If (DESC) {[dict setobject: [DESC stringvalue] forkey: kname_desc];} // <deal_tips> ddxmlelement * tips = [deal elementforname: kname_tips]; If (TIPS) {[dict setobject: [tips stringvalue] forkey: kname_tips] ;}} [array addobject: dict] ;}return array ;}// request abort (error)-(void) requestfailed :( asihttprequest *) request {[HUD removefromsuperview]; nserror * error = [Request Error]; nslog (@ "requesterror: % @", [error description]);} @ end
The most important thing here is to parse the returned XML data, and the comments in the Code are also detailed. I think you should be able to understand it, after obtaining the data, we can display it in aqgridview. This content is detailed in the third article.