//请求jSON数据
//<1>创建请求操作管理者对象
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
//<2>设置请求的数据类型(json、xml)
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
//<3>开始请求数据并解析
[manager GET:kJSONUrlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//此时responseObject就是解析以后的数据 如果json数据根是字典那么responseObject就是字典 如果是数组那么responseObject就是数组
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error.description);
}];
//xml数据请求
//<1>创建请求操作管理者对象
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
//<2>设置请求数据的类型(json、xml)
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/xml"];
//<3>AFNetworking 提供的xml数据解析非常复杂 一般的时候都是用第三方库GData 所以关闭AFN提供的xml解析
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//<4>开始请求数据
[manager GET:kXMLUrlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//<1>将所有数据解析出来放在内存中
GDataXMLDocument * document = [[GDataXMLDocument alloc]initWithData:responseObject options:0 error:nil];
//<2>根据路径查找想要的节点信息
NSArray * array = [document nodesForXPath:@"//title" error:nil];
//<3>遍历数组中的节点内容
for(GDataXMLElement * element in array)
{
//获取节点的内容调用的方法是stringValue
NSLog(@"%@",element.stringValue);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error.description);
}];
2.多任务请求数据
NSArray * imageArr = @[@"http://icon.58pic.com/revision/images/img-code-bg.png",
@"http://icon.qiantucdn.com/img/searchnew/wechat-g.png",
@"http://icon.qiantucdn.com/revision/images/user-img-default.png"];
//【注意】一个请求对象只能请求一个接口的数据
//定义一个可变数组存放三个请求对象 然后将请求对象放在队列中
NSMutableArray * requestArr = [[NSMutableArray alloc]init];
for(int i = 0;i<3;i++)
{
//<1>将字符串接口转化成NSURL
NSURL * url = [NSURL URLWithString:imageArr[i]];
//<2>将NSURL封装成请求对象
NSURLRequest * request = [NSURLRequest requestWithURL:url];
//<3>将请求对象封装成请求操作对象
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
//<4>对象请求数据结束以后将内容显示在UIImageView上
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
//将responseObject NSData类型的数据转化成UIImage
UIImage * image = [UIImage imageWithData:responseObject];
UIImageView * imageView = (UIImageView *)[self.view viewWithTag:i + 1];
imageView.image = image;
NSLog(@"request%d",i + 1);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error.description);
}];
//<5>将请求对象放在数组中
[requestArr addObject:operation];
}
//<6>创建队列对象
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
//<7>设置最大请求数据的对象个数
queue.maxConcurrentOperationCount = 3;
//<8>将所有请求对象放在队列中
[queue addOperations:requestArr waitUntilFinished:NO];
判断网络状态
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//<2>判断网络状态
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if(status == AFNetworkReachabilityStatusReachableViaWiFi)
{
NSLog(@"WIFI");
}
else if (status == AFNetworkReachabilityStatusReachableViaWWAN)
{
NSLog(@"3G/4G/GPRS");
}
else if (status == AFNetworkReachabilityStatusNotReachable)
{
NSLog(@"无网络连接");
}
else
{
NSLog(@"网络未知");
}
}];
//<3>开启网络测试
[manager.reachabilityManager startMonitoring];
网友评论