zoukankan      html  css  js  c++  java
  • iOS AFNetworking 2.x JSON方法

    写在前面(废话,可略过)又是好久没更新,主要是忙(懒)。争取多学习,多分享!

    正式开始我们在开发的过程中,难免会遇到网络操作,我们可以使用iOS原装的网络框架,当然了,使用三方框架更容易些,非常出名的就是AFNetworking框架,这个框架貌似还是12年最佳三方框架。总之,iOS开发,网络部分,基本都会用到这个框架。
    这个框架在github上,AFNetworking 传送门 --> 点我点我
    如果使用此框架的1.x版本,在JSON解析方面使用的某一个API,但是在2.x以后,AFNetworking推荐使用另一个API,而网上大部分教程都是1.x JSON API,此文主要分享下2.x的方法。

    先让大家感受下1.x的

    //构建网址
        NSString *urlString = [NSString stringWithFormat:@"xxxxxxxxxx"];//xxxx处写一个你的网址
     
        //如果网址中有中文,需要转换
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     
        //构建NSURL
        NSURL *url = [NSURL URLWithString:urlString];
        //构建请求,这个构建方法是基本构建方法的一个封装加强。主要多了超时属性,就是最后一个参数,4.0f。意思就是如果在4秒内没有响应,就不阻塞主线程(为啥放主线程就不赘述了)
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:4.0f];

        //以AF开始的是就是AFNetworking框架的API,这是1.x的方法。
        AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:requestsuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            NSDictionary *dictionary = JSON;
            int ret = [[dictionary objectForKey:@"ret"] intValue];
          
            //对服务器返回数据进行判断
            switch (ret) {
                case -1:
                    [self showAlertWithString:@"用户未登录"];
                    break;
                case 1:
                    [self saveDataToCurrentAccount];
                    [self showAlertWithDelegateWithString:@"保存成功"];
                    break;
                case -2:
                    [self showAlertWithString:@"保存失败"];
                    break;
            }
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
            [self showAlertWithString:[NSString stringWithFormat:@"%@", error.localizedDescription]];
        }];
        [op start];

    相信大家都能看懂,
    它很好的使用了block,如果成功,todo,如果失败,tudo。(成功、失败里面的方法是我自己封装的,你写你自己的就OK)
    并且整体方法放入operation。
    直接把返回的JSON以参数形式给你。

    这就是1.x的JSON方法,但是如果使用的是2.x的框架,再使用这个方法就会报错,因为2.x的框架取消了AFJSONRequestOperation
    这个时候我们应该怎么做呢?


    让大家感受下2.x的
    NSString *urlString = [NSString stringWithFormat:@"xxxxxxxx"];
        //有中文,需要转换
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:4.0f];

        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        op.responseSerializer = [AFJSONResponseSerializer serializer];
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            NSDictionary *dictionary = responseObject;
            int ret = [[dictionary objectForKey:@"ret"] intValue];

            //对服务器返回数据进行判断
            switch (ret) {
                case -1:
                    [self showAlertWithString:@"用户未登录"];
                    break;
                case 1:
                    [self saveDataToCurrentAccount];
                    [self showAlertWithDelegateWithString:@"保存成功"];
                    break;
                case -2:
                    [self showAlertWithString:@"保存失败"];
                    break;
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self showAlertWithString:[NSString stringWithFormat:@"%@", error.localizedDescription]];

        }];
        [[NSOperationQueue mainQueue] addOperation:op];


    在2.x中,他使用了AFHTTPRequestOperation和AFJSONResponseSerializer,对结果用他自己的API序列化,这时候,没有id JSON了,但是你把responseObject当 id JSON用就OK了。


    最后提提觉得大家应该都知道,但是还是写出来吧。
    如何导入框架呢?
    给两个方法吧
    一。直接去传送门找需要的下载,一般我们使用第一个,也就是默认的AFNetworking框架。进去后,看右侧,有个download,下载、解压,里面有个文件夹,找找,就是框架的.h.m,具体文件结构忘了,不过很好找的。然后把这个文件夹拖入工程,选copy,group,你的target。然后在需要使用的地方import AFNetworking.h 就OK。
    二。使用cocoaPods配置
  • 相关阅读:
    stm32的hal之串口库函数总结复习
    关闭win10 任务栏窗口预览的步骤:
    sizeof的注意点
    goto语句——慎用,但是可以用
    #define的一个小技巧
    Chapter 1 First Sight——36
    Chapter 1 First Sight——35
    Chapter 1 First Sight——34
    leetcode409
    Chapter 1 First Sight——34
  • 原文地址:https://www.cnblogs.com/guanmingweng/p/4191309.html
Copyright © 2011-2022 走看看