网络数据获取的封装

来源:互联网 发布:foxmail mac邮件位置 编辑:程序博客网 时间:2024/05/16 11:54

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

#import <Foundation/Foundation.h>@protocol NetworkingToolDelegate <NSObject>-(void)bringResult:(id)result;@endtypedef 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;
0 0
原创粉丝点击