[iPhone中级]iPhone团购信息客户端的开发 (二)
接上一篇,这篇我们对我们的客户端加入KissXML,MBProgressHUD,AQridView这几个库,首先我们先加入KissXML,这是XML解析库,支持Xpath,可以方便添加更改任何节点。先从官方网站上下载KissXML这个库
接下来,将KissXML库中的KissXML文件夹拷入我们的工程中,如图
然后跟上次一样,我们通过File/Add File to "Tuan1"这个选项加入到我们的工程中,我们以同样的方法分别加入MBProgressHUD和AGridView这两个库
在Framework中加入QuartzCore.framework,因为AGridView需要用到。
接着我们在代码中加入
#import"MBProgressHUD.h"
#import"DDXML.h"
这二行,运行一下,如果没有报错的话,那这几个库我们就加入成功了。
接着我们更改一下storybord,将FirstViewController这个视图加入NavigationController,选中FirstViewController在主菜单Editor/Embed In/Navigation Controller,系统自动为我们加入NavigationController。
下面,我们将FirstViewController中清空,所有内容都删除
好了,到这一步,我们的视图基本都完成了,接下来就是编码了,首先我们来解析美团网团购信息
FirstViewController.h
[plain]
#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
@interface KKFirstViewController : UIViewController<ASIHTTPRequestDelegate>
@end
#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
@interface KKFirstViewController : UIViewController<ASIHTTPRequestDelegate>
@end
FirstViewController.m
[plain]
#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 = @"美团";
//背景
UIImage *bgImage = [UIImage imageNamed:@"bg-app.png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage];
//HUD提示框
hud = [[MBProgressHUD alloc] init];
hud.labelText = @"载入数据...";
//网址
NSString *urlString = @"http://www.meituan.com/api/v2/beijing/deals";
[hud showWhileExecuting:@selector(startHttpRequest:) onTarget:self withObject:urlString animated:YES];
//因为hud需要显示在最前面,所以我们在NavigationController中加入
[self.navigationController.view addSubview:hud];
arrays = [NSMutableArray array];
}
//请求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)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//请求结束
-(void)requestFinished:(ASIHTTPRequest *)request{
//移除提示框
[hud removeFromSuperview];
//返回解析后的数据,每一个Array包括一个字典
arrays = [self xmlParser:[request responseData]];
NSLog(@"%@", arrays);
}
//解析结点
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 = @"deal_img";
static NSString *kName_Desc = @"deal_desc";
static NSString *kName_Tips = @"deal_tips";
//XML解析
-(NSMutableArray *)xmlParser:(NSData *)data{
NSMutableArray *array = [NSMutableArray array];
//文档开始
DDXMLDocument *xmlDoc = [[DDXMLDocument alloc] initWithData:data options:0 error:nil];
//返回<data>中的所有元素
NSArray *items = [xmlDoc nodesForXPath:kXPath_Item error:nil];
//遍历每个元素
for (DDXMLElement *item in items) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//返回<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;
}
//请求中止(出错)
-(void)requestFailed:(ASIHTTPRequest *)request{
[hud removeFromSuperview];
NSError *error = [request error];
NSLog(@"RequestError : %@", [error description]);
}
@end
作者:kangkangz4
相关新闻>>
- 发表评论
-
- 最新评论 更多>>