异步POST请求及加载信息到UitabView上
2014-07-27 21:26
302 查看
有一个New类,有三个属性name(title),summary和newsUrl;JSON解析的是一个字典套数组套字典的形式。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.arr = [NSMutableArray
array];
// 1.
建立请求
//
获取文件路径
NSURL *url = [NSURL
URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
//
建立请求对象
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];
//
设定请求的方式
request.HTTPMethod =
@"POST";
//
设定POST请求的Body
//
需要提交的表单
NSString *bodyStr =
@"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
//
将bodyStr转为NSData
NSData *bodyData = [bodyStr
dataUsingEncoding:NSUTF8StringEncoding];
//
设置请求的body(NSData类型)
request.HTTPBody = bodyData;
// 2.异步连接服务器 Block回调
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue
mainQueue] completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError) {
// 将结果转为字符串
NSString *result = [[NSString
alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"-----%@", result);
NSMutableDictionary *dic = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:nil];
NSMutableArray *array = [dic
objectForKey:@"news"];
// 遍历数组
for (NSDictionary *dictionary
in array) {
News *new = [[News
alloc] initWithNSDictionary:dictionary];
[self.arr
addObject:new];
// 重新加载数据
[self.tableView
reloadData];
[new
release];
}
}];
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view.
self.tableView = [[UITableView
alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
self.tableView.separatorColor = [UIColor
purpleColor];
self.tableView.delegate =
self;
self.tableView.dataSource =
self;
[self.tableView
registerClass:[UITableViewCell
class] forCellReuseIdentifier:@"1"];
[self.view
addSubview:self.tableView];
[_tableView
release];
}
// New类对象的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arr
count];
}
// cell里的内容为title
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"1"];
cell.textLabel.text = [[self.arr
objectAtIndex:indexPath.row]
name];
return cell;
}
// 推出下一个页面,将New类对象传过去
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
SecondViewController *second = [[SecondViewController
alloc]
init];
second.newObject = [self.arr
objectAtIndex:indexPath.row];
[self.navigationController
pushViewController:second animated:YES];
[second
release];
}
{
[super
viewDidLoad];
// Do any additional setup after loading the view.
//
接收MainViewController传来的New对象
UITextView *textView = [[UITextView
alloc] initWithFrame:self.view.bounds];
textView.text =
self.newObject.summary;
textView.editable =
NO;
[self.view
addSubview:textView];
[textView
release];
// webView的显示
UIWebView *webView = [[UIWebView
alloc] initWithFrame:CGRectMake(0,
320, 320,
160)];
//
路径是newsUrl,
是一个字符串, 将它进行加载
NSURL *url = [NSURL
URLWithString:self.newObject.newsUrl];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];
//
加载请求
[webView
loadRequest:request];
[self.view
addSubview:webView];
[webView
release];
}
{ "news": [ { "rownum": 1, "id": "B664817CFB08FCE", "title": "京郊避暑游达到峰值 周末农家客栈不预定住不上", "type": "北京新闻", "cid": "213", "cname": "北京新闻", "newsUrl": "http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/Site_PV_Count.ashx?newsId=B664817CFB08FCE&createDate=20140727&PID=213&UpdateTime=1406459313", "typeId": "01", "sequence": 1, "attribute": 0, "lastUpdateTime": "1406459313", "PUBLISHDATE": "20140727", "picUrl": "http://ipad-bjwb.bjd.com.cn/DigitalPublication/MediaLibrary/20140727/B664817CFB08FCE/80755750BBA178D8.jpg", "summary": " 2014年7月27日 北京 进入三伏天,北京周边的河畔和山谷就成了许多市民周末纳凉的去处。这个周末,高达35℃的天气又将不少城里人“赶”到京郊去,不少出京路段甚至出现了拥堵。昨天一大早,京承路往密云、怀柔去的方向就从四环一直堵到了六环,这其中,不少是到京郊纳凉避暑的市民。“我们8点出发,本以为不晚,谁知道已经开始堵了。”昨天市民刘先生带着全家老少出行,在京承路足足堵了两个多小时,车行缓慢。“现在订房得排到8月底了”雾灵山脚下的新城子村宋晓梅家的农家客栈住宿已经爆满,这里既有市民全家出游,也有不少企业组织员工到京郊避暑。由于人多,客栈厨师人手短缺,已经从村里临时借了两个厨工帮忙。院主告诉记者,进入暑期已经不分周末还是工作日了,几乎天天客满,周六、日人更多,如果不提前预定,这一带都找不到住宿。“今年水量并不大,可是游客还是比预计的要多。”京北第一漂景区昨天一天就涌进了2000多游客。虽然今年的水量很少,并不是玩漂流最好的时候,但还是有不少游客愿意到水中嬉戏。“山里的温度平均比城里低5℃,早晚更凉快,住在这里晚上还要盖薄被。”延庆珍珠泉村的农家院昨天也全部挤满了城里避暑的市民,有不少市", "commentCount": 0 },
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.arr = [NSMutableArray
array];
// 1.
建立请求
//
获取文件路径
NSURL *url = [NSURL
URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
//
建立请求对象
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];
//
设定请求的方式
request.HTTPMethod =
@"POST";
//
设定POST请求的Body
//
需要提交的表单
NSString *bodyStr =
@"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
//
将bodyStr转为NSData
NSData *bodyData = [bodyStr
dataUsingEncoding:NSUTF8StringEncoding];
//
设置请求的body(NSData类型)
request.HTTPBody = bodyData;
// 2.异步连接服务器 Block回调
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue
mainQueue] completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError) {
// 将结果转为字符串
NSString *result = [[NSString
alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"-----%@", result);
NSMutableDictionary *dic = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:nil];
NSMutableArray *array = [dic
objectForKey:@"news"];
// 遍历数组
for (NSDictionary *dictionary
in array) {
News *new = [[News
alloc] initWithNSDictionary:dictionary];
[self.arr
addObject:new];
// 重新加载数据
[self.tableView
reloadData];
[new
release];
}
}];
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view.
self.tableView = [[UITableView
alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
self.tableView.separatorColor = [UIColor
purpleColor];
self.tableView.delegate =
self;
self.tableView.dataSource =
self;
[self.tableView
registerClass:[UITableViewCell
class] forCellReuseIdentifier:@"1"];
[self.view
addSubview:self.tableView];
[_tableView
release];
}
// New类对象的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arr
count];
}
// cell里的内容为title
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"1"];
cell.textLabel.text = [[self.arr
objectAtIndex:indexPath.row]
name];
return cell;
}
// 推出下一个页面,将New类对象传过去
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
SecondViewController *second = [[SecondViewController
alloc]
init];
second.newObject = [self.arr
objectAtIndex:indexPath.row];
[self.navigationController
pushViewController:second animated:YES];
[second
release];
}
secondViewController.m
- (void)viewDidLoad{
[super
viewDidLoad];
// Do any additional setup after loading the view.
//
接收MainViewController传来的New对象
UITextView *textView = [[UITextView
alloc] initWithFrame:self.view.bounds];
textView.text =
self.newObject.summary;
textView.editable =
NO;
[self.view
addSubview:textView];
[textView
release];
// webView的显示
UIWebView *webView = [[UIWebView
alloc] initWithFrame:CGRectMake(0,
320, 320,
160)];
//
路径是newsUrl,
是一个字符串, 将它进行加载
NSURL *url = [NSURL
URLWithString:self.newObject.newsUrl];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];
//
加载请求
[webView
loadRequest:request];
[self.view
addSubview:webView];
[webView
release];
}
相关文章推荐
- volley 应用 GET POST请求 图片异步加载(全面)
- 关于融云(设置用户信息的提供者)异步请求 图片加载问题
- volley 应用 GET POST请求 图片异步加载
- volley 应用 GET POST请求 图片异步加载
- Android加载WebView - 使用post请求加载
- Android volley 应用 GET POST请求 图片异步加载
- webView 加载页面的两种请求方式: get 和 post
- iOS开发之WebView怎么加载post请求并且传参数
- volley 应用 GET POST请求 图片异步加载
- 异步懒人加载图片(GET和POST请求方式)
- volley 应用 GET POST请求 图片异步加载
- WebView加载网页保存信息到Cookie
- curl post 中文内容及请求头信息的修改方法
- UITableView异步加载图片+缓存
- uiimageview 异步加载图片
- 判断用户的请求类型 get or post 然后同步 或者 异步 执行 方法
- System.Reflection.ReflectionTypeLoadException: 无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性。
- 使用GirdView异步加载图片,并对图片进行颜色矩阵变化
- iPhone UITableView异步加载图片
- UIImageView异步加载网络图片