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异步请求的更多相关文章
- iOS中发送HTTP请求的方案
在iOS中,常见的发送HTTP请求的方案有 苹果原生(自带) NSURLConnection:用法简单,最古老最经典的一种方案 NSURLSession:功能比NSURLConnection更加强大, ...
- iOS NSURLConnection POST异步请求封装,支持转码GBK,HTTPS等
.h文件 #import <Foundation/Foundation.h> //成功的回调 typedef void(^successBlock)(id responseObj); // ...
- Springmvc中 同步/异步请求参数的传递以及数据的返回
转载:http://blog.csdn.net/qh_java/article/details/44802287 注意: 这里的返回就是返回到jsp页面 **** controller接收前台数据的方 ...
- Jquery中Ajax异步请求中的async参数的作用
之前不知道这个参数的作用,上网找了前辈的博客,在此收录到自己的博客,希望能帮到更多的朋友: test.html <a href="javascript:void(0)" on ...
- springmvc中同步/异步请求参数的传递以及数据的返回
注意: 这里的返回就是返回到jsp页面 **** controller接收前台数据的方式,以及将处理后的model 传向前台***** 1.前台传递数据的接受:传的属性名和javabean的属性相同 ...
- for循环中嵌套异步请求问题
for循环中嵌套了异步请求会导致顺序错乱,用递归代替for循环,可以保证正常执行顺序:
- CAT中实现异步请求的调用链查看
CAT简介 CAT(Central Application Tracking),是美团点评基于 Java 开发的一套开源的分布式实时监控系统.美团点评基础架构部希望在基础存储.高性能通信.大规模在线访 ...
- iOS中的网络请求 和 网络监测
1.网络监测 //根据主机名判断网络是否连接 Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu ...
- IOS中UITableView异步加载图片的实现
本文转载至 http://blog.csdn.net/enuola/article/details/8639404 最近做一个项目,需要用到UITableView异步加载图片的例子,看到网上有一个E ...
随机推荐
- Android AChartEngine 去除折线图黑边
通常使用AChartEngine画出的折线图,如果背景不是黑色,则会在折线图的坐标轴旁边出现黑边,如图所示: 试了好多设置,最后终于发现,去除黑边的设置是: mRenderer.setMarginsC ...
- Apache Shiro 使用手册(一)Shiro架构介绍
一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户"登录": 授权 - ...
- [platform]linux platform device/driver(三)--Platform Device和Platform_driver注册过程之代码对比
转自:http://blog.csdn.net/thl789/article/details/6723350 Linux 2.6的设备驱动模型中,所有的device都是通过Bus相连.device_r ...
- !!!!!122. Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- PHP中的Libevent学习
wangbin@2012,1,3 目录 Libevent在php中的应用学习 1. Libevent介绍 2. 为什么要学习libevent 3. Php libeven ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- chrome控制台查看控件有没绑定事件[转]
chrome控制台查看btn_comment_submit控件有没绑定事件 function lookEvents (elem) { return $.data ? $.data( elem, ...
- Linux学习笔记——切换并取代用户身份命令——su
再次从头好好的学习Linux,本着以免轻易忘记,以备后用的原则,将我觉得常用或者好玩的linux命令记录在这,注意:我的实验环境是 Ubuntu 14.04.1 su 这个命令我经常使用,因为 ...
- 使用 Git 管理源代码
在现代软件开发项目中,要成为一个有效的软件开发人员,我们必须能够与其他项目贡献者并行进行开发.源代码管理(SCM)系统不是什么新思想.为了编写一些能够更快速.简单地开发以后软件项目的软件,已经进行了很 ...
- Form文件夹开发步骤
1.开发完一个Form,测试OK 1.添加Object Groups 操作如下:在同一窗口打开标准的Form APPSTAND.FMB 和我们自己客制的Form,并且选择APPSTAND.FMB的Ob ...