• AFNetworking序列化与网络监测


    AFNetworking内部会自动反序列化,默认是解析JSON

    -(void)json
    {
        //1.创建会话管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        //2.get请求
        NSDictionary *dict = @{
                               @"username":@"afn",
                               @"pwd":@"afn",
                               @"type":@"JSON",
                               };
        
        //AFN内部会自动进行反序列化处理(JSON)
        [manager GET:@"http://120.25.226.186:32812/login" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"成功--%@---%@",responseObject,[responseObject class]);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"失败-=-%@",error);
        }];
        
    }
    

    服务器返回的是XML的数据格式时的解析

    -(void)xml
    {
        //1.创建会话管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        //告诉AFN以XML的方式来解析数据
        manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        
        //2.get请求
        NSDictionary *dict = @{
                               @"type":@"XML",
                               };
        
        //AFN内部会自动进行反序列化处理(JSON)
        [manager GET:@"http://120.25.226.186:32812/video" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSXMLParser *parser) {
    //        NSLog(@"成功--%@---%@",responseObject,[responseObject class]);
            
            //设置代理
            parser.delegate = self;
            
            //开始解析
            [parser parse];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"失败-=-%@",error);
        }];
    }
    

    解析xml数据的代理方法

    pragma mark ----------------------
    #pragma mark NSXMLParserDelegate
    //elementName:返回文件名;attributeDict:文件属性
    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
    {
        NSLog(@"%@--%@",elementName,attributeDict);
    }
    

    HTML网页的解析

    -(void)httphtml
    {
        //1.创建会话管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        //设置不解析,一服务器返回的格式接收
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        //2.get请求
        //AFN内部会自动进行反序列化处理(JSON)
        [manager GET:@"http://www.baidu.com" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"--%@---%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding],[responseObject class]);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"失败-=-%@",error);
        }];
    }
    

    AFN网络监测

    -(void)afn
    {
        //1.创建网络监测管理者
        AFNetworkReachabilityManager  *manager = [AFNetworkReachabilityManager sharedManager];
        
        //2.检测
        /*
         AFNetworkReachabilityStatusUnknown          = -1,   未知
         AFNetworkReachabilityStatusNotReachable     = 0,    没有网络
         AFNetworkReachabilityStatusReachableViaWWAN = 1,    蜂窝网络3G|4G
         AFNetworkReachabilityStatusReachableViaWiFi = 2,    WIFI
         */
        [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    NSLog(@"wifi");
                    break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    NSLog(@"蜂窝网络3G|4G");
                    break;
                case AFNetworkReachabilityStatusNotReachable:
                    NSLog(@"没有网络");
                    break;
                case AFNetworkReachabilityStatusUnknown:
                    NSLog(@"未知");
                    break;
                default:
                    break;
            }
        }];
        
        //3.开始监测
        [manager startMonitoring];
    
    }
    

    系统自带的网络监测
    要导入Reachability头文件

    #import "ViewController.h"
    #import "AFNetworking.h"
    #import "Reachability.h"
    
    @interface ViewController ()<NSXMLParserDelegate>
    /** Reachability*/
    @property (nonatomic ,strong)Reachability *r;
    @end
    
    @implementation ViewController
    
    -(void)viewDidLoad
    {
        [super viewDidLoad];
    
        //注册通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChange) name:kReachabilityChangedNotification object:nil];
        
        self.r = [Reachability reachabilityForInternetConnection];
        [self.r startNotifier];
    }
    
    -(void)reachabilityChange
    {
        /*
         NotReachable = 0,  没有网络
         ReachableViaWiFi,  WIFI
         ReachableViaWWAN   蜂窝网络
         */
        //首先获得一个Reachability 有状态的
        if([Reachability reachabilityForInternetConnection].currentReachabilityStatus == ReachableViaWWAN)
        {
            NSLog(@"蜂窝网络");
            return;
        }
        
        if ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == ReachableViaWiFi) {
            NSLog(@"WIFI");
            return ;
        }
        
        NSLog(@"没有网络");
    }
    

    注册通知要移除监听对象并且Reachability要调用stopNotifier方法

    -(void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [self.r stopNotifier];
        
    }
    
  • 相关阅读:
    查看Eclipse版本号的方法
    设置Eclipse的字体风格方式
    又遇两个小异常
    我所推崇的三种心态
    关于javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt;
    Http请求中Content-Type讲解
    ftp实现文件上传(下载)
    解析html文档的java库及范例
    xslt循环转换子元素
    XPath学习:轴(1)——child
  • 原文地址:https://www.cnblogs.com/xzk-it/p/6603165.html
Copyright © 2020-2023  润新知