[NSURLConnection]分别用Post和Get方式获取网络数据并把数据显示到表格
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UIButton* getButton;
UIButton* postButton;
UITableView* table;
NSMutableArray* array;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; getButton=[[UIButton alloc]initWithFrame:CGRectMake(, , , )];
getButton.backgroundColor=[UIColor orangeColor];
[getButton setTitle:@"GET" forState:UIControlStateNormal];
[getButton addTarget:self action:@selector(getData) forControlEvents:UIControlEventTouchUpInside]; postButton=[[UIButton alloc]initWithFrame:CGRectMake(, , , )];
postButton.backgroundColor=[UIColor orangeColor];
[postButton setTitle:@"POST" forState:UIControlStateNormal];
[postButton addTarget:self action:@selector(postData) forControlEvents:UIControlEventTouchUpInside]; table=[[UITableView alloc]initWithFrame:CGRectMake(, , , -) style:UITableViewStylePlain];
table.dataSource=self;
table.delegate=self;
[self.view addSubview:table];
[self.view addSubview:getButton];
[self.view addSubview:postButton];
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return array.count;
} #pragma mark 表示每一行显示什么数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//内存优化
static NSString * identity=@"cell";
//tableview 根据标识复制出一个cell
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identity];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identity];
}
NSDictionary* dic=array[indexPath.row];
cell.textLabel.text=[dic valueForKey:@"title"];
cell.detailTextLabel.text=[dic valueForKey:@"type"];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} -(void)getData
{
NSLog(@"get");
//异步请求
//创建NSString用来存储请求的网址
NSString* str=@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
//用UTF8String格式转换成NSURL
NSURL* url=[NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//创建请求
NSURLRequest* request=[[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:];
//发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
//处理数据
NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:data options: error:nil];
array=[dic valueForKey:@"news"];
[table reloadData];
}
if (connectionError) {
NSLog(@"%@",[connectionError description]);
}
}];
} -(void)postData
{
NSLog(@"post");
//异步请求
//创建NSString用来存储请求的网址
NSString* str=@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
//用UTF8String格式转换成NSURL
NSURL* url=[NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//创建请求
NSMutableURLRequest* request=[[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:];
[request setHTTPMethod:@"POST"];
//设置参数
NSString* where=@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData* data=[where dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
//发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
//处理数据
NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:data options: error:nil];
array=[dic valueForKey:@"news"];
[table reloadData];
}
if (connectionError) {
NSLog(@"%@",[connectionError description]);
}
}];
}
[NSURLConnection]分别用Post和Get方式获取网络数据并把数据显示到表格的更多相关文章
- [NSURLSession/Delegate]用Post方式获取网络数据并把数据显示到表格
#pragma mark 实现NSURLSessionDataDelegate代理 @interface ViewController ()<UITableViewDataSource,UITa ...
- Http方式获取网络数据
通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据.(sax解析主要是解析xml)具体代码如下: package com.wy ...
- 用 get 同步/异步 方式获取网络数据并输出
//同步请求 //创建NSString用来存储请求的网址 NSString* str=@"http://v.juhe.cn/weather/index?format=2&cityna ...
- android—获取网络数据
取网络数据主要靠发交易(或者说请求,接口等),而这些交易由java中的网络通信,HttpURLConnection和HttpClient实现,以下是具体例子. 大家都知道,网络通信,发送请求有两种 ...
- Windows Phone 同步方式获取网络类型
原文:Windows Phone 同步方式获取网络类型 在Windows Phone 开发中有时候需要获取设备当前连接网络的类型,是Wifi,还是2G,3G,或者4G,SDK中提供获取网络类型的API ...
- Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》
在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...
- 使用GET与POST方式获取html数据
抓取网站数据解析的工作,其中,使用到GET和POST方法获取html数据. 使用GET方式: /** * 使用get方式获取html数据 * * @param strURL(需要访问的网站) * @r ...
- 使用promise方式来获取网络数据
获取网络数据 let data = []; new Promise(function(resolve,reject){ axios.post('api.php').then(function(resp ...
- Swift - 异步获取网络数据封装类
使用NSURLConnection.sendAsynchronousRequest()可以采用异步获取的方式取得数据.下面通过对数据获取类进行封装,演示如何进行数据请求与接收. 1,HttpContr ...
随机推荐
- html 等页面防止中文出现乱码的终极解决方案
网页UTF-8中文乱码问题解决方法 网页UTF-8中文乱码问题解决方法只有经过多方面测试的东西才有质量的保证和说服力,之前一直都是在本地做开发,经过本地测试也是通过的,但一发布到远程服务器上就问题百出 ...
- linux命令--dig
dig,和nslookup作用有些类似,都是DNS查询工具,但是却比nslookup强大 dig,其实是一个缩写,即Domain Information Groper. [我想用google-DNS来 ...
- Redis在WEB开发中的应用与实践
Redis在WEB开发中的应用与实践 一.Redis概述: Redis是一个功能强大.性能高效的开源数据结构服务器,Redis最典型的应用是NoSQL.但事实上Redis除了作为NoSQL数据库使用之 ...
- 深入理解Javascript面向对象编程
深入理解Javascript面向对象编程 阅读目录 一:理解构造函数原型(prototype)机制 二:理解原型域链的概念 三:理解原型继承机制 四:理解使用类继承(继承的更好的方案) 五:建议使用封 ...
- [原] Android性能优化方法
GPU过度绘制 打开开发者选型,"调试GPU过度绘制",蓝.绿.粉红.红,过度绘制依次加深 粉红色尽量优化,界面尽量保持蓝绿颜色 红色肯定是有问题的,不能忍受 使用Hierarch ...
- Java并发包源码学习之AQS框架(四)AbstractQueuedSynchronizer源码分析
经过前面几篇文章的铺垫,今天我们终于要看看AQS的庐山真面目了,建议第一次看AbstractQueuedSynchronizer 类源码的朋友可以先看下我前面几篇文章: <Java并发包源码学习 ...
- Shanghai Regional Online Contest 1004
Shanghai Regional Online Contest 1004 In the ACM International Collegiate Programming Contest, each ...
- 2015多校1006.First One
First One Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- Javascript正则表达式匹配替换
根据正则表达式的匹配结果将匹配项替换为*function regReplace(reg, str){ var result, //最终输出结果 out, //每次运行正则exec返回的匹配结果. in ...
- 吉他笔记 solo 和弦 推弦 音程
十二平均律: 如下图所示: 第一行为唱名:do re mi fa so.... 第二行为音名:C #C D #D E F #F G #G A #A B C 第三行为D调对应的音名,即1 = D 第四行 ...