/*************** 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);
}];
}
[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); //此处就可以对数据进行具体解析
}];
[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封装:
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(@"请求失败,请检查网络");
}];