iOS中POST异步请求

POST异步请求(代理)json

一、遵循<NSURLConnectionDataDelegate>api

@interface ViewController ()<NSURLConnectionDataDelegate>

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

@property (nonatomic,strong)NSMutableData *reDtata;

三、获取url异步

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

四、建立request请求post

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

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

request.HTTPMethod = @"POST";

六、设置HTTPBody(url中的body部分,若是body部分含有中文须要转化)url

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

七、建立链接并设置代理spa

  [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
{
    
}

 

 

 

下面是实现的全部代码code

- (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
相关文章
相关标签/搜索