今天在学习项目的时候,涉及关于网络资源下载的第三方API。现在就陈述下今天的学习经过
1.ASIHTTPRequest 是什么?
简单地说,可以通过这个API让我们下载网络资源,网页什么的
2.如何添加第三方API?
下载地址:点击打开链接
1).将Classes和External文件夹添加到工程
2).按照配置1和2进行配置 build setting-> Header Search Paths 为 $(SDK_DIR)/usr/include/libxml2
build setting-> Other Linker FLags 为 -lxml2
3).添加类库 libxml2, libz.1.2.5,MobileCoreServices,SystemConfiguration,CFNetwork ,注意,如果想使用此API,这几个包必须添加到库中。
3.结合项目陈述操作步骤
1.在viewDidLoad中添加网络通知:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate =self;
self.tableView.dataSource =self;
self.title = self.selectIndex;
// Do any additional setup after loading the view from its nib.
//网络通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
}
2.判断我们的当前网络是否正常
//缓冲的文件判断和当前网络的判断
- (void)viewWillAppear:(BOOL)animated
{
NSString *isCache = [NSString stringWithContentsOfFile:[self pathFile] encoding:NSUTF8StringEncoding error:nil];
if (isCache) {
[self jsonParse];
}
else
{
int netCheck = [self showState]; //根据当前的网络状态设置,这个showState方法是很重要的:i
if (netCheck == 0) {
self.tableView.hidden = YES;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(113, 363, 85, 30)];
label.tag = 501;
label.text = @"无网络连接";
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
}
else
{
UILabel *label = (UILabel *)[self.view viewWithTag:501]; //如果有网络,那么就去掉 无网络连接label
label.hidden = YES;
if (self.key == 1)
[self pyRequestData]; //进行网络加载 iii
else
[self bsRequestData];
self.tableView.hidden = NO;
}
}
}
i:网络检查 showState方法
//网络检查
-(int) showState
{
//检查当前网络状态 引入了外部接口。。。。。//这里的Reachablity就是我们API接口的一部分
Reachability *currentReachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus state = [currentReachability currentReachabilityStatus];
switch (state) {
case NotReachable:// II:
return 0;
break;
case ReachableViaWiFi:
return 1;
break;
case ReachableViaWWAN:
return 2;
break;
}
return -1;
}
ii:这是状态枚举
enum {
// Apple NetworkStatus Constant Names.
NotReachable = kNotReachable,
ReachableViaWiFi = kReachableViaWiFi,
ReachableViaWWAN = kReachableViaWWAN
};
iii:进行网络加载 ,这里是加载的格式以及后面触发的协议方法
//拼音请求数据
- (void)pyRequestData
{
NSString *string = [[NSString alloc]initWithFormat:@"http://www.chazidian.com/service/pinyin/%@/0/1000",self.selectIndex ];
//拼接需要查询的地址
NSURL *url = [NSURL URLWithString:string];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:self.cache]; //设置缓存
request.delegate = self; //触发代理方法 -> 3
[request startAsynchronous];
}
3.代理的方法
#pragma mark ASIHTTPRequestdelegate 方法
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
self.muData = [NSMutableData dataWithCapacity:10];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self.muData appendData:[request responseData]]; //进行数据的储存
if (self.muData == nil) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(113 , 363, 85, 30)];
label.tag = 400;
label.text = @"客官,网络不给力啊!";
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
}else
{
UILabel *label = (UILabel *)[self.view viewWithTag:400];
label.hidden = YES;
[self.muData writeToFile:[self pathFile] atomically:YES];
self.mutArray = [NSMutableArray arrayWithCapacity:10];
[self jsonParse]; //这就是JS解析了~
}
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"%@",request.error);
}
4.整体看来是不是很清晰呢?
在我们的viewdidLoad中,我们加入了通知时间,但是通知里面的一个方法我们没有实现,因为API已经实现了那个方法。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
+ (void)reachabilityChanged:(NSNotification *)note
{
[bandwidthThrottlingLock lock];
isBandwidthThrottled = [ASIHTTPRequest isNetworkReachableViaWWAN];
[bandwidthThrottlingLock unlock];
}