封装NetWorkingTool

1.协议方法,负责传数据.因为传值需要两种情况,所以是id类型

<span style="font-size:18px;">- (void)bringResult:(id)result;</span>

2.然后根据方法调用传过来的网址,获取数据并且解析数据.因为是类方法,没有对象,所以代理人直接在函数里设置

<span style="font-size:18px;">// 根据方法调用时候传过来的网址,获取数据,并且解析数据,负责解析数据
+ (void)networkingWithStrURL:(NSString *)strURL delegate:(id<NetworkingToolDelegate>)delegate;
// 加号方法,没有对象,所以指定代理人时要这么写</span>

3.正常的解析数据(block会执行的比较慢,没有办法写return,因为block有延时性,所以需要用协议或者block传值)

<span style="font-size:18px;">+ (void)networkingWithStrURL:(NSString *)strURL delegate:(id<NetworkingToolDelegate>)delegate{
    //
    NSURL *url = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // block是会执行的比较慢,没有办法写return,因为block有延时性,所以需要用协议或者block传值
        // JCD
        // 找到当前工程的主线程
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        // 异步将子线程内容显示到主线程
        dispatch_async(mainQueue, ^{
            // 数据解析
            id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            // 代理人去执行协议方法
            [delegate bringResult:result];
        });
    }];
    [task resume];</span>
}

4.创建数据的方法中调用函数  

<span style="font-size:18px;">//    [NetworkingTool networkingWithStrURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" delegate:self];</span>

5.实现协议方法

<span style="font-size:18px;">// 协议方法
- (void)bringResult:(id)result{
    NSLog(@"%@", result);
}</span>

1.block传值,传值的时候传一个block,先将block改名成(Block),block(result)通过block把值返回

// block传值
// 把block改新的名字 叫Block
typedef void(^Block)(id result);

// block传值
+ (void)networkingWithStrURL:(NSString *)strURL block:(Block)block;

// block传值, 模仿系统block
+ (void)networkingWithStrURL:(NSString *)strURL block:(Block)block{
    NSURL *url = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_queue_t queue = dispatch_get_main_queue();
        dispatch_async(queue, ^{
            id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            // 通过block把值返回
            block (result);
            
        });
    }];
    [task resume];

}


3.(1)post,GET方法两种,需要判断.先建一个枚举,表示类型

// POST,GET
typedef NS_ENUM(NSUInteger, MethodType) {
    GETType,
    POSTType,
};

(2)方法中需要添加类型,bodyStr,还有block

// 在方法添加
+ (void)networkingWithStrURL:(NSString *)strURL  type:(MethodType)type bodyStr:(NSString *)bodyStr block:(Block)block;

(3)实现

+ (void)networkingWithStrURL:(NSString *)strURL type:(MethodType)type bodyStr:(NSString *)bodyStr block:(Block)block{
    NSURL *url = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 先判断类型,如果是POST需要三句话
    if (type == POSTType) {
        [request setHTTPMethod:@"POST"];
        NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data];
       
    }
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        block(result);
        
        
        
    }];
    [task resume];

}

(4)主函数实现

//    // GET和POST两种网络请求
//   [NetworkingTool networkingWithStrURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" type:GETType bodyStr:nil block:^(id result) {
//       NSLog(@"%@", result);
//   }];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值