iOS开发:GET与POST接口网络请求以及对AFNetworking的二次封装
2015-09-27 11:26
941 查看
/*************** iOS开发:GET 和 POST接口网络请求 *******************/
-、网络请求的两种方式:GET 和 POST
1.普通封装方法:封装到一个类中
1、自定义一个block
typedefvoid (^DataBlock)
(id data);
2、自定义GET 、POST方法
+ (void)getDataByURLString:(NSString *)urlString WithDataBlock:(DataBlock)dataBlock;
+ (void)getDataByURLString:(NSString *)urlString HttpMethod:(NSString *)method BodyString:(NSString *)bodyString DataBlock:(DataBlock)datablock;
3、实现GET、POST方法
+ (void)getDataByURLString:(NSString *)urlString WithDataBlock:( DataBlock)dataBlock{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
dataBlock(data);
}];
}
+ (void)getDataByURLString:(NSString *)urlString HttpMethod:(NSString *)method BodyString:(NSString *)bodyString DataBlock:(DataBlock)datablock{
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//默认是get方法,修改成post
if ([method isEqual:@"POST"]) {
[request setHTTPMethod:method];
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
}
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
datablock(data);
}];
}
在ViewController中直接调用类(+)方法:
get请求:
[HttpMethod getDataByURLString:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html" WithDataBlock:^(id data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dic); //此处就可以对数据进行具体解析
}];
post请求:
[HttpMethod getDataByURLString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx" HttpMethod:@"POST" BodyString:@"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213"
DataBlock:^(id data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dic);//此处就可以对数据进行具体解析
(1).下载并引入AFNetWorking头文件:
直接在viewController中:
a.创建AF管理对象:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
b.设置网址(接口请求数据)符合的类型:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/json", @"application/json", @"text/javascript", @"text/html",
nil];
c.进行数据解析:
get方法:
[manager GET:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败,请检查网络"); //此处就可以对数据进行具体解析
}];
post方法:
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"20131129", @"date", @"1", @"startRecord", @"30", @"len", @"1234567890", @"udid", @"Iphone",
@"terminalType", @"213", @"cid", nil];
[manager GET:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@", responseObject); //此处就可以对数据进行具体解析
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败,请检查网络");
}];
-、网络请求的两种方式:GET 和 POST
1.普通封装方法:封装到一个类中
1、自定义一个block
typedefvoid (^DataBlock)
(id data);
2、自定义GET 、POST方法
+ (void)getDataByURLString:(NSString *)urlString WithDataBlock:(DataBlock)dataBlock;
+ (void)getDataByURLString:(NSString *)urlString HttpMethod:(NSString *)method BodyString:(NSString *)bodyString DataBlock:(DataBlock)datablock;
3、实现GET、POST方法
+ (void)getDataByURLString:(NSString *)urlString WithDataBlock:( DataBlock)dataBlock{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
dataBlock(data);
}];
}
+ (void)getDataByURLString:(NSString *)urlString HttpMethod:(NSString *)method BodyString:(NSString *)bodyString DataBlock:(DataBlock)datablock{
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//默认是get方法,修改成post
if ([method isEqual:@"POST"]) {
[request setHTTPMethod:method];
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
}
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
datablock(data);
}];
}
在ViewController中直接调用类(+)方法:
get请求:
[HttpMethod getDataByURLString:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html" WithDataBlock:^(id data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dic); //此处就可以对数据进行具体解析
}];
post请求:
[HttpMethod getDataByURLString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx" HttpMethod:@"POST" BodyString:@"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213"
DataBlock:^(id data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dic);//此处就可以对数据进行具体解析
}];
二、AFNetWorking封装:(1).下载并引入AFNetWorking头文件:
直接在viewController中:
a.创建AF管理对象:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
b.设置网址(接口请求数据)符合的类型:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/json", @"application/json", @"text/javascript", @"text/html",
nil];
c.进行数据解析:
get方法:
[manager GET:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败,请检查网络"); //此处就可以对数据进行具体解析
}];
post方法:
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"20131129", @"date", @"1", @"startRecord", @"30", @"len", @"1234567890", @"udid", @"Iphone",
@"terminalType", @"213", @"cid", nil];
[manager GET:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@", responseObject); //此处就可以对数据进行具体解析
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败,请检查网络");
}];
相关文章推荐
- hdu 5475 An easy problem (2015 上海网络赛 H题)
- 用反射封装HttpHandler,实现通过action方法名调用方法
- 关于用了一天时间在虚拟机下共享网络的问题说明
- HDU 5475 An easy problem(网络赛水题)
- HDU 5475 An easy problem(网络赛水题)
- 5477 A Sweet Journey (贪心_上海网络赛)
- RedHat Linux网络配置详解
- tcp处理
- 校招笔试面试计算机网络部分的常考点
- Tengine TCP 负载均衡
- 计算机网络之网络安全
- 计算机网络之网络安全
- 计算机网络缩略语
- 网络文件系统
- 40. HTTP 协议(1)
- android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类). 用xUtils会出现问题
- SNMP简单网络管理协议
- Postman用法简介-Http请求模拟工具
- 线段树 2015上海网络赛 An easy probleam
- TCP建立与终止