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
  • 相关阅读:
    基于小程序开发的藏书馆
    picker(级联)组件及组件封装经验
    秒杀组件开发-可实现多种倒计时功能
    async/await 与 generator、co 的对比
    nodejs项目总结
    小程序开发小结-线下服务器域名部署等
    性能提速:debounce(防抖)、throttle(节流/限频)
    vuex数据管理-数据模块化
    vue 项目其他规范
    vue路由管理-保留滚动位置功能、按需加载模块名自定义
  • 原文地址:https://www.cnblogs.com/cityingma/p/4897984.html
Copyright © 2011-2022 走看看