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 ...
随机推荐
- BizTalk 中使用 WCF-OracleDB adapter
在使用BizTalk WCF-OracleDB adapter操作Oracle数据库时,遇到了一些问题,记录如下. 按照BizTalk的文档,目前BizTalk 2010支持的Oracle数据库版本如 ...
- INNO 补丁制作技术, 打开 INNO 补丁制作方法的第一页
INNO 补丁制作技术, 打开 INNO 补丁制作方法的第一页 作者:xin 日期:2005-09-23 字体大小: 小 中 大 VPatch 在 INNO 中的应用. VPatch 属于专为NS ...
- PHP编译过程中常见错误信息的解决方法
在CentOS编译PHP5的时候有时会遇到以下的一些错误信息,基本上都可以通过yum安装相应的库来解决.以下是具体的一些解决办法: checking for BZip2 support… yes ch ...
- Hibernate5.2之HQL查询
Hibernate5.2之HQL查询 一. 介绍 Hibernate的 ...
- windows 代理服务器的搭建,提供Android 端访问公网.
这段时间遇到一个情况,移动的网络收费.但是可以访问学校内部的网络,比如说学校官网图书馆之类了.所以我这里便想到一个方法,用学校内部一个可以访问互联网的主机充当代理服务器(我这里使用自己的电脑,非服务器 ...
- css如何实现多行文本时,内容溢出,出现省略号
一:单行文本出现省略号: .oneLine{ white-space: nowrap; text-overflow: ellipsis; overflow: hidden; width: 100px; ...
- C Primer Plus(第五版)12
第 12 章 存储类, 链接和内存管理 在本章中你将学习下列内容 . 关键字: auto, extern, static, register, const, volatile, restricted. ...
- jquery.easypiechart.js简介
此插件主要是用来统计新的访问.跳出率.服务器负载.使用的RAM等,功能很强大,带有HTML5的动画效果,效果非常炫,看效果吧easyPieChart一款新型的EASY饼图数据统计Jquery插件截图: ...
- Spark Streaming、Kafka结合Spark JDBC External DataSouces处理案例
场景:使用Spark Streaming接收Kafka发送过来的数据与关系型数据库中的表进行相关的查询操作: Kafka发送过来的数据格式为:id.name.cityId,分隔符为tab zhangs ...
- oracle中的查询语句(关于出库入库信息表,明细表,把捆包箱表,单位信息表的集中查询)
--查出所有现金中心的单位IDwith AllUnitas(select t.ORGANIZATIONID orgid,t.parentidfrom CDMS_ORGANIZATION t where ...