zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    查询datatime类型
    ms的题目,无聊不妨看看
    读取客户端收藏夹资料的问题
    delphi中的DBGrid无法刷新数据
    jsp与javascript
    .net2.0 web site中的cs文件怎么编译为dll
    由传奇木马引起的遐想
    com组件的调用
    Crystal Report的奇怪问题
    算法导论15章LCS实现(c++)
  • 原文地址:https://www.cnblogs.com/cityingma/p/4897984.html
Copyright © 2011-2022 走看看