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
相關文章
相關標籤/搜索