NetWorkTool工具类之网络请求
2015-10-01 14:17
459 查看
NetWorkTool.h
#pragma mark这个类通过block的方法,把这个类请求的数据,返回到视图控制器.
typedef void(^ Block)(id result);
@interface NetWorkTool : NSObject
-(void)netWorkingWithURL:(NSString *)strURL block:(Block)block;
+(void)netWorkingWithURL:(NSString *)strURL block:(Block)block;
//实现POST请求
+(void)netWorkingWithURL:(NSString *)strURL body:(NSString *)strbody block:(Block)block;
NetWorkTool.h
-(void)netWorkingWithURL:(NSString *)strURL block:(Block)block{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
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];
//把json处理好的数据,通过block进行回调,返回到视图控制器
block(result);}];}
+(void)netWorkingWithURL:(NSString *)strURL block:(Block)block{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
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];
//把json处理好的数据,通过block进行回调,返回到视图控制器
block(result);}];}
+(void)netWorkingWithURL:(NSString *)strURL body:(NSString *)strbody block:(Block)block{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSData *data1=[strbody dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data1];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {id result=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:nil];
block(result);}];}
#pragma mark这个类通过block的方法,把这个类请求的数据,返回到视图控制器.
typedef void(^ Block)(id result);
@interface NetWorkTool : NSObject
-(void)netWorkingWithURL:(NSString *)strURL block:(Block)block;
+(void)netWorkingWithURL:(NSString *)strURL block:(Block)block;
//实现POST请求
+(void)netWorkingWithURL:(NSString *)strURL body:(NSString *)strbody block:(Block)block;
NetWorkTool.h
-(void)netWorkingWithURL:(NSString *)strURL block:(Block)block{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
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];
//把json处理好的数据,通过block进行回调,返回到视图控制器
block(result);}];}
+(void)netWorkingWithURL:(NSString *)strURL block:(Block)block{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
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];
//把json处理好的数据,通过block进行回调,返回到视图控制器
block(result);}];}
+(void)netWorkingWithURL:(NSString *)strURL body:(NSString *)strbody block:(Block)block{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSData *data1=[strbody dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data1];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {id result=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:nil];
block(result);}];}
相关文章推荐
- 构建高性能WEB之HTTP首部优化
- httpget
- TCP/IP协议原理与应用笔记11:TCP/IP中地址与层次关系
- TCP浅谈(上)
- TCP/IP相关知识点
- 理解HTTP/304响应
- 妈咪212网络搜索功能,打开它
- c# HttpWebRequest 代码手动设置cookies的简单办法
- HTTP协议简述
- 构架https服务器
- http header 详解
- zeromq源码分析–TCP连接处理流程
- 网络流模板
- 网络编程--IO模型示例
- C++实现网络编程 服务端与客户端原代码
- CentOS首次安装,网络环境配置
- angular 使用$http.jsonp进行跨域访问以及修改目标JSON
- http://code.662p.com/view/5141.html
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- Delphi IDHTTP用法详解(六种用法)