JSON
#import "ViewController.h"
#import "YYHttpTool.h"// 网络请求工具类,基于AFN的封装
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self JSON];
}
//AFN内部默认已经做了反序列化处理
-(void)JSON
{
//1.创建会话管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//参数单独写出来
NSDictionary *dictM = @{@"type":@"JSON"};
//2.发请求
[manager GET:@"http://120.25.226.186:32812/video" parameters:dictM progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//通过打印下面的代码可知,内部已经把JOSN数据转换层OC对象(字典)。即用responseObject表示出来,也就是服务器返回给访问者的数据类型是字典类型的数据
NSLog(@"成功--%@",responseObject);
NSLog(@"成功--%@",[responseObject class]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"失败--%@",error);
}];
}
@end
截图:
XML
#import "ViewController.h"
#import "YYHttpTool.h"// 网络请求工具类,基于AFN的封装
@interface ViewController ()<NSXMLParserDelegate>
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self XML];
}
-(void)XML
{
//1.创建会话管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//修改对响应体信息的解析方式(默认是用JSON解析数据,即1),修改为用XML解析数据(即2).如果注释2,则打印NSData数据
//manager.responseSerializer = [AFJSONRequestSerializer serializer];//1
manager.responseSerializer = [AFXMLParserResponseSerializer serializer];//2
NSDictionary *dictM = @{@"type":@"XML"};
//2.发请求
[manager GET:@"http://120.25.226.186:32812/video" parameters:dictM progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//NSLog(@"成功--%@",responseObject);
NSXMLParser *parser = (NSXMLParser *)responseObject;
//设置代理
parser.delegate = self;
//开始解析
[parser parse];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"失败--%@",error);
}];
}
#pragma mark NSXMLParserDelegate
//开始解析某个元素的时候回调用,并且这个方法会被调用多次
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
NSLog(@"%@--%@",elementName,attributeDict);
}
@end
截图:
访问图片
#import "ViewController.h"
#import "YYHttpTool.h"// 网络请求工具类,基于AFN的封装
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self Picture];
}
//发送访问图片的请求
-(void)Picture
{
//1.创建会话管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//修改数据的解析方式
//3种解析方式:JSON & XML &http(不做任何处理)
manager.responseSerializer = [AFHTTPResponseSerializer serializer];//用XML解析数据
//2.发请求
[manager GET:@"http://120.25.226.186:32812/resources/images/minion_02.png" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"成功--%@",[responseObject class]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"失败--%@",error);
}];
}
@end
打印结果
2016-08-14 18:27:23.398 AFN之访问图片 [15180:1110061] 成功--_NSInlineData
访问网页
#import "ViewController.h"
#import "YYHttpTool.h"// 网络请求工具类,基于AFN的封装
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self Website];
}
//(发送访问网页的请求)
-(void)Website
{
//1.创建会话管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//内容类型为text/html
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//2.发请求(发送登录网页的请求)
[manager GET:@"http://www.baidu.com" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//内部已经把JOSN数据转换层OC对象(字典)
NSLog(@"成功--%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);//把二进制数据responseObject转换成字符串NSString
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"失败--%@",error);
}];
}
@end
截图:
网络状态
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
[self NetworkStatus];
}
-(void)NetworkStatus{
//1.通过网络监测管理者监听状态的改变
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
/*
AFNetworkReachabilityStatusUnknown = -1, 未知
AFNetworkReachabilityStatusNotReachable = 0, 没有网络
AFNetworkReachabilityStatusReachableViaWWAN = 1, 3G|4G
AFNetworkReachabilityStatusReachableViaWiFi = 2, WIFI
*/
switch (status) {
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WIFI");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G&4G");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"没有网络");
break;
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知");
break;
default:
break;
}
}];
//2.开始监听
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
@end
打印结果