网络数据获取的封装
来源:互联网 发布: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
- 网络数据获取的封装
- 获取网络数据的封装
- 网络连接的判断的封装与 网络获取数据 的封装
- 网络连接的判断与封装以及网络获取数据的封装
- 封装传值的接口与获取网络数据
- 获取网络判断和数据进行封装
- 一个从网络获取数据的封装类,以及接口回调
- OkHttp网络请求数据的封装
- 从mysql获取数据方法的封装
- python suds 获取webService封装的数据
- tableView的网络数据获取
- json封装,获取数据
- 获取数据封装
- 网络请求数据工具封装
- Android自定义网络数据加载等待框的简单封装
- 获取网络上的json数据
- Android中网络获取数据的方法
- 简单的获取网络数据 测试使用
- mysql的binlog太大
- Installing Jenkins on Red Hat distributions
- 【C++】STL--QUEUE
- HDU 1016 Prime Ring Problem(DFS)
- 《剑指offer》——反转链表
- 网络数据获取的封装
- 原来的账号不用了。
- Java 实现下载
- win10开机后内存占用高
- Android Handler removeMessages引发postDelayed失效的问题
- CSS雪碧
- myeclipse安装git插件
- cocos2dx-js 编译android包的方法
- golang 组合和接口