zoukankan      html  css  js  c++  java
  • ios开发网络学习二:URL转码以及字典转模型框架MJExtension的使用

    一:url转码,当url中涉及到中文的时候,要考虑转码,用UTF8对中文的url进行转码

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    #pragma mark ----------------------
    #pragma mark Events
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        [self post];
    }
    
    #pragma mark ----------------------
    #pragma mark Methods
    -(void)get
    {
        
        NSString *urlStr = @"http://120.25.226.186:32812/login2?username=小码哥&pwd=520it&type=JSON";
        
        NSLog(@"转码前: %@",urlStr);
        
        //中文转码处理
        urlStr =  [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        
        NSLog(@"转码后: %@",urlStr);
        
        //1.url
        NSURL *url = [NSURL URLWithString:urlStr];
        
        //http://120.25.226.186:32812/login2?username=%E5%B0%8F%E7%A0%81%E5%93%A5&pwd=520it&type=JSON
        
        NSLog(@"url------%@",url);
        
        //2.urlrequest
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        //3.connect
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            
            
            //容错处理
            if (connectionError) {
                NSLog(@"%@",connectionError);
                return ;
            }
            //4.解析
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        }];
    
    }
    
    -(void)post
    {
        //观察URL中是否有中文,如果有中文则需要转码
        NSString *urlStr = @"http://120.25.226.186:32812/login2";
        
        //username=小码哥&pwd=520it&type=JSON
        //1.url
        NSURL *url = [NSURL URLWithString:urlStr];
        
        //2.urlrequest
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        
        //2.1 post
        request.HTTPMethod = @"POST";
        
        //2.2 body
        request.HTTPBody = [@"username=小码哥&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
        
        //3.connect
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            
            //容错处理
            if (connectionError) {
                NSLog(@"%@",connectionError);
                return ;
            }
            //4.解析
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        }];
    
    }
    @end

    二:MJExtension的使用

    1:字典转模型框架

    1)相关框架

         a.Mantle 需要继承自MTModel

         b.JSONModel 需要继承自JSONModel

         c.MJExtension 不需要继承,无代码侵入性

    (2)自己设计和选择框架时需要注意的问题

        a.侵入性

        b.易用性,是否容易上手

        c.扩展性,很容易给这个框架增加新的功能

    (3):MJExtension的使用

    1:字典转模型:

    1://file:是读取本地的数据本地的资源包存在NSBundle中,url是获取网络端的数据

        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"person" ofType:@"plist"]];

        XMGPerson *person = [XMGPerson mj_objectWithKeyValues:dict];

    2:当网络数据既有字典又有数组字典的时候:需要在模型中重写:

    + (NSDictionary *)mj_objectClassInArray方法。以下两种方试都可以

    + (NSDictionary *)mj_objectClassInArray

    {

        // key : 属性名

        // value : 类名

        return @{

                 @"dogs" : @"XMGDog",

                 @"books" : @"XMGBook"

                 };

    //    return @{

    //             @"dogs" : [XMGDog class],

    //             @"books" : [XMGBook class]

    //             };

    }

     3:当需要改变网络返回数据的key值时:如遇到系统关键字id或是description时,需要在模型中重写

    + (NSDictionary *)mj_replacedKeyFromPropertyName

    + (NSDictionary *)mj_replacedKeyFromPropertyName

    {

        // 属性名-字典key-映射

        // key : 属性名

        // value : 字典中的key

        return @{

                 @"desc" : @"description",

                 @"ID" : @"id",

                 @"name" : @[@"name", @"screenname"],//表示属性name既可以对应name也可以对应screenname,优先查看name的值若是name的值没有,则查看screenname的值

                 @"info" : @[@"other.info[1].abc",//此种情况的数据情况如图:点语法是为字典的key赋值,[]表示数组中的第几个对象,.abc表示这个对象下的test

                             @"other.info[0].test",

                             @"name"]

                 };

    }

     

    注意:如果服务器返回的数据最外层是字典,字典中又有字典A,字典A中又有其他字典,数据结构如图:

    解决办法:1:可以用字典转模型,将other字典再构造一个模型 ,字典转模型  2:还可以在数据model中重写+ (NSDictionary *)mj_replacedKeyFromPropertyName方法,

    + (NSDictionary *)mj_replacedKeyFromPropertyName

    {

        // 属性名-字典key-映射

        // key : 属性名

        // value : 字典中的key

        return @{

                 @"desc" : @"description",

                 @"ID" : @"id",

                 @"name" : @[@"name", @"screenname"],//表示属性name既可以对应name也可以对应screenname,优先查看name的值若是name的值没有,则查看screenname的值

                 @"info" : @"other.info" 

                 };

    }

    4:MJExtension在项目中的应用:一般为了减少 MJExtension对项目的侵入性,基本做法都是新建配置类继承NSObject,在配置类中实现+(void)load方法,在此方法中对模型中的字段进行配置

    #import "XMGExtensionConfig.h"
    #import "XMGUser.h"
    #import "XMGBook.h"
    #import "XMGDog.h"
    #import <MJExtension.h>
    #import "XMGOther.h"
    #import "XMGPerson.h"
    
    @implementation XMGExtensionConfig
    
    + (void)load
    {
    #pragma mark - XMGUser
        [XMGUser mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{
                     @"desc" : @"description",
                     // @"ID" : @"id",
                     @"name" : @[@"name", @"screenname"],
                     @"info" : @[@"other.info[1].abc",
                                 @"other.info[0].test",
                                 @"name"]
                     };
        }];
        
        [XMGUser mj_setupObjectClassInArray:^NSDictionary *{
            return @{
                     @"dogs" : @"XMGDog",
                     @"books" : @"XMGBook"
                     };
        }];
        
    #pragma mark - XMGPerson
        [XMGPerson mj_setupReplacedKeyFromPropertyName121:^NSString *(NSString *propertyName) {
            // if ([propertyName isEqualToString:@"ID"]) return @"id";
            return [propertyName mj_underlineFromCamel];
        }];
        
    #pragma mark - XMGBook
    //    [XMGBook mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
    //        return @{@"ID" : @"id"};
    //    }];
        
    #pragma mark - XMGDog
    //    [XMGDog mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
    //        return @{@"ID" : @"id"};
    //    }];
        
        [NSObject mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{@"ID" : @"id"};
        }];
    }
    
    @end

    注意:1:在配置类中进行数据模型的配置,需要实现+(void)load方法,在此方法内进行配置,不需要导入头文件,因为+(void)load是在类加载进内存就会被调用,而且只会被调用一次,+(void)initialize 是累呗使用的时候调用,也是被调用一次 2:若是向一些参数模型,或是数据模型等含有重复的字段值,1:则可以抽重复的字段值抽成父类,让子类去继承,此时父类也可以提供初始化方法给子类继承,在父类初始化方法中父类可以为一些固定的属性赋值  2:用父类去进行初始化,也就是如上面代码所示,都含有ID的配置,则可以用父类NSObject去配置,因为任何类都是继承自NSObject,则此时子类就相当于配置了ID,它的原理就是oc的对象机制,先优先从子类中寻找,子类要是有此属性,就不再去父类寻找,子类若是没有此属性,则其会去父类中查找。

  • 相关阅读:
    抗战纪念日之前
    学习VS生活
    来到这里,我放弃了多少- UI基础-疯狂猜图,我们都疯狂了-
    开班典礼-老师玩命的教,大家玩命的学,沉静,18K
    见老同学记
    6月第一周学习总结
    临界区和不变量和谓词
    C库得到系统当前的时间
    查看一个进程有多少个线程
    bubble chart|Matrix Scatter|Overlay Scatter|Scatterplots|drop-line|box plot|Stem-and-leaf plot|Histogram|Bar chart|Pareto chart|Pie chart|doughnut chart|
  • 原文地址:https://www.cnblogs.com/cqb-learner/p/5860811.html
Copyright © 2011-2022 走看看