网络数据获取的封装

可以直接用,无需再重新写, 下面是三个封装,用一个就可以
NetworkingTool.h

#import <Foundation/Foundation.h>

@protocol NetworkingToolDelegate <NSObject>

-(void)bringResult:(id)result;

@end

typedef void(^Block)(id result);

typedef NS_ENUM(NSUInteger, MethodType) {
    GETType,
    POSTType,

};
@interface NetworkingTool : NSObject

//根据方法调用时传来的网址,获取数据,并且解析数据
+(void)networkingWithStrURL:(NSString *)strURL delegate:(id<NetworkingToolDelegate>)delegate;

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


+(void)networkingWithStrURL:(NSString *)strURL type:(MethodType)type bodyStr:(NSString *)bodyStr block:(Block)block ;



@end

NetworkingTool.m

#import "NetworkingTool.h"


@implementation NetworkingTool

+(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) {
        dispatch_queue_t mainQuenue=dispatch_get_main_queue();
        dispatch_async(mainQuenue, ^{
            id result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            //代理人去执行协议方法
            [delegate bringResult:result];
        });
    }];
    //任务执行
    [task resume];
}


+(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];
}


+(void)networkingWithStrURL:(NSString *)strURL type:(MethodType)type bodyStr:(NSString *)bodyStr block:(Block)block{
    NSURL *url=[NSURL URLWithString:strURL];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    if (type==POSTType) {
        [request setHTTPMethod:@"POST"];
        NSData *bodyData=[bodyStr dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:bodyData];
    }
    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(result);
        });

    }];
    [task resume];




}

用的时候直接调用就好,只需要写下面这句

-(void)createData{
//    [NetworkingTool networkingWithStrURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" delegate:self];



//    [NetworkingTool networkingWithStrURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(id result) {
//        NSLog(@"%@",result);
//    }];


    [NetworkingTool networkingWithStrURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" type:GETType bodyStr:nil block:^(id result) {
        self.HUD.hidden=YES;

        //scrollView自适应高度
//        self.contentLabel.text=result[@"events"][0][@"content"];
//
//        self.contentLabel.numberOfLines=0;
//        [self.contentLabel sizeToFit];
//        NSLog(@"%g",self.contentLabel.frame.size.height);
//        self.scroll.contentSize=CGSizeMake(0, 300+self.contentLabel.frame.size.height);


        NSLog(@"%@",result);

    } ];


}

补充一个MBProgressHUD
用于产生加载视图,就是一直转的loading
只需要把文件拖进工程,引一下头文件

#import "MBProgressHUD.h"

先写一个属性

@property(nonatomic,retain)MBProgressHUD *HUD;

再创建

self.HUD=[MBProgressHUD showHUDAddedTo:self.tableView animated:YES];

最后在解析数据完成后把动画效果隐藏

self.HUD.hidden=YES;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值