GET请求
#import "RootViewController.h"
@interface RootViewController ()<NSURLConnectionDataDelegate>
@property(nonatomic,strong)UIButton *getButton;
// 声明一个属性,用来接受请求下来的data
@property(nonatomic,strong)NSMutableData *myData;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.getButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.getButton.frame = CGRectMake(100, 100, 100, 100);
[self.getButton setTitle:@"get请求" forState:UIControlStateNormal];
[self.view addSubview:self.getButton];
[self.getButton addTarget:self action:@selector(getButtonAction:) forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view.
}
- (void)getButtonAction:(UIButton *)sender{
// GET请求 同步
// [self p_getData];
// GET请求 异步使用代理
// [self p_getData2];
// GET请求 异步使用Block
[self p_getData3];
}
- (void)p_getData{
// 1. 准备URL
NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchcinema.php?cinemaId=141"];
NSURL *url = [NSURL URLWithString:urlStr];
// 2. 准备请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
// 设置请求方法
[request setHTTPMethod:@"GET"];
// 3. 准备数据
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// 4. 解析
NSError *error = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",dict);
}
- (void)p_getData2{
// 1 准备URL
NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchcinema.php?cinemaId=141"];
NSURL *url = [NSURL URLWithString:urlStr];
// 2. 准备请求
NSMutableURLRequest *reguest = [[NSMutableURLRequest alloc]initWithURL:url];
// 3. 准备连接(使用代理方法 实现异步请求)
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:reguest delegate:self];
// // 4. 开始连接
// [connection start];
// NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:reguest delegate:self startImmediately:YES];
}
- (void)p_getData3{
// 1. 准备URL
NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchcinema.php?cinemaId=141"];
NSURL *url = [NSURL URLWithString:urlStr];
// 2. 准备请求
NSMutableURLRequest *reguest = [[NSMutableURLRequest alloc]initWithURL:url];
// 3. 使用Block异步解析
[NSURLConnection sendAsynchronousRequest:reguest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dict);
}];
}
#pragma mark GET第二种异步代理方法
// 1. 接受到响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
self.myData = [NSMutableData data];
}
// 2. 接收到数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.myData appendData:data];
}
// 3. 已经完成加载
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.myData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dict);
}
POST请求 (可以设置参数)
#import "RootViewController.h"
@interface RootViewController ()<NSURLConnectionDataDelegate>
@property(nonatomic,strong)UIButton *postButton;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.postButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.postButton.frame = CGRectMake(100, 100, 100, 100);
[self.postButton setTitle:@"post请求" forState:UIControlStateNormal];
[self.view addSubview:self.postButton];
[self.postButton addTarget:self action:@selector(postButtonAction:) forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view.
}
- (void)postButtonAction:(UIButton *)sender{
// post 同步
// [self p_postData];
// post 异步
[self p_postData1];
}
- (void)p_postData{
NSLog(@"Post请求(同步)");
NSString *urlStr = [NSString stringWithFormat:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *reguest = [[NSMutableURLRequest alloc]initWithURL:url];
[reguest setHTTPMethod:@"POST"];
// 准备参数
NSString *argument = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
NSData *argumentData = [argument dataUsingEncoding:NSUTF8StringEncoding];
// 设置URL的参数
[reguest setHTTPBody:argumentData];
NSData *data = [NSURLConnection sendSynchronousRequest:reguest returningResponse:nil error:nil];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dict);
}
- (void)p_postData1{
// 1. 准备URL
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
// 2. 准备请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc ]initWithURL:url];
[request setHTTPMethod:@"POST"];
// 3. 设置参数
NSString *argument = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
// 将字符串参数装换成NSData
NSData *argData = [argument dataUsingEncoding:NSUTF8StringEncoding];
// 将参数添加到URL
[request setHTTPBody:argData];
// 准备连接
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dict);
}];
}