UI17_NetWorkingTool

来源:互联网 发布:淘宝客服的流程 编辑:程序博客网 时间:2024/06/07 12:15

封装的NetWorkingTool, 包含get, post请求, 协议, block
ViewController.h

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end

ViewController.m

#import "ViewController.h"#import "NetWorkingTool.h"@interface ViewController ()<NetWorkingToolDelegate>@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.////    NSString *strURL = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";////    NetWorkingTool *tool = [[NetWorkingTool alloc] init];//    //  调用方法////    [tool netWorkingGetProtocol:strURL];//    //    NetWorkingTool *tool = [[NetWorkingTool alloc] init];//    NSString *strURLPost = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";//    NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";//    //    [tool netWorkingPostProtocol:strURLPost BodyStr:bodyStr];//    //  设置代理人//    tool.delegate = self;    [NetWorkingTool getNetWorking:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" delegate:self];//    [NetWorkingTool getNetWorking:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(id result) {//        NSLog(@"%@", result);//    }];//    [NetWorkingTool postNetWorking:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx" bodyStr:@"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213" block:^(id result) {//        NSLog(@"%@", result);//    }];//    //    NSArray *arr = @[@"1", @"2", @"3", @"4", @"5"];//    ////    [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {//        NSLog(@"%@", obj);//    }];}- (void)takeValue:(id)result {    NSDictionary *dic = result;    NSLog(@"****%@", dic);}

NetWorkingTool.h

#import <Foundation/Foundation.h>@protocol NetWorkingToolDelegate <NSObject>- (void)takeValue:(id)result;@end//  从后往前传值, 没有返回值, 有参数typedef void(^Block)(id result);@interface NetWorkingTool : NSObject@property(nonatomic, assign)id<NetWorkingToolDelegate>delegate;- (void)netWorkingGetProtocol:(NSString *)strURL;- (void)netWorkingPostProtocol:(NSString *)strURL BodyStr:(NSString *)bodyStr;+ (void)getNetWorking:(NSString *)strURL delegate:(id<NetWorkingToolDelegate>)delegate;+ (void)getNetWorking:(NSString *)strURL block:(Block)block;+ (void)postNetWorking:(NSString *)strURL bodyStr:(NSString *)bodyStr block:(Block)block;@end

NetworkingTool.m

#import "NetWorkingTool.h"@implementation NetWorkingTool- (void)netWorkingGetProtocol:(NSString *)strURL {    //  创建URL    NSURL *url = [NSURL URLWithString:strURL];    //  创建一个请求    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        //  json解析        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];        //  让代理人去执行协议方法, 把解析好的值返回到调用的视图控制器        [self.delegate takeValue:result];    }];}- (void)netWorkingPostProtocol:(NSString *)strURL BodyStr:(NSString *)bodyStr {    NSURL *url = [NSURL URLWithString:strURL];    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    //  设置请求方式    [request setHTTPMethod:@"POST"];    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];    [request setHTTPBody:bodyData];    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];        [self.delegate takeValue:result];    }];}+ (void)getNetWorking:(NSString *)strURL delegate:(id<NetWorkingToolDelegate>)delegate {    NSURL *url = [NSURL URLWithString:strURL];    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];        [delegate takeValue:result];    }];}+ (void)getNetWorking:(NSString *)strURL block:(Block)block {    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]];    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];        //  调用block, 进行传值        block(result);    }];}+ (void)postNetWorking:(NSString *)strURL bodyStr:(NSString *)bodyStr block:(Block)block {    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]];    [request setHTTPMethod:@"POST"];    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];    [request setHTTPBody:bodyData];    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];        block(result);    }];}@end
0 0
原创粉丝点击