您的位置:首页 > 理论基础 > 计算机网络

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);//此处就可以对数据进行具体解析

}];

二、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(@"请求失败,请检查网络");

}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: