iOS中POST异步请求

POST异步请求(代理)

1、遵循<NSURLConnectionDataDelegate>

@interface ViewController ()<NSURLConnectionDataDelegate>

2、NSMutableData类型的reData属性是用来拼接数据的

@property (nonatomic,strong)NSMutableData *reDtata;

3、获取url

 NSString *urlString = @"http://api.tudou.com/v3/gw";
    NSURL *url = [NSURL URLWithString:urlString];

4、创建request请求

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

5、设置HTTPMethod为POST请求(默认为GET请求)

request.HTTPMethod = @"POST";

6、设置HTTPBody(url中的body部分,如果body部分含有中文需要转化)

 NSString *bodyStr = @"method=album.channel.get&appKey=myKey&format=json&channel=c&pageNo=1&pageSize=15";
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = bodyData;

7、创建连接并设置代理

  [NSURLConnection connectionWithRequest:request delegate:self];

8、实现代理方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.reDtata = [NSMutableData data];
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_reDtata appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:_reDtata options:(NSJSONReadingAllowFragments) error:nil];
    NSLog(@"%@",dic);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
}

 

 

 

下面是实现的所有代码

- (IBAction)postAsyc:(id)sender{}是从storyboard里面拖出来的控件代码,也可以直接写代码实现,写一个button和它的实现方法即可。
#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
@property (nonatomic,strong)NSMutableData *reDtata;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)postAsyc:(id)sender {
    NSString *urlString = @"http://api.tudou.com/v3/gw";
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    NSString *bodyStr = @"method=album.channel.get&appKey=myKey&format=json&channel=c&pageNo=1&pageSize=15";
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = bodyData;
    [NSURLConnection connectionWithRequest:request delegate:self];
    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.reDtata = [NSMutableData data];
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_reDtata appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:_reDtata options:(NSJSONReadingAllowFragments) error:nil];
    NSLog(@"%@",dic);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

你可能感兴趣的:(iOS中POST异步请求)