码迷,mamicode.com
首页 > 移动开发 > 详细

IOS--POST异步请求

时间:2016-03-02 22:03:22      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

TestController.m

 1 #import "TestController.h"
 2 
 3 @interface TestController()
 4 
 5 @property(nonatomic,strong)UIButton *button;
 6 
 7 @end
 8 
 9 @implementation TestController
10 
11 - (void)viewDidLoad
12 {
13     [super viewDidLoad];
14     
15     _button = [UIButton buttonWithType:UIButtonTypeSystem];
16     
17     _button.frame = CGRectMake(0, 20, 100, 20);
18     [_button setTitle:@"Hello" forState:UIControlStateNormal];
19     
20     [_button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
21 
22     
23     [self.view addSubview:_button];
24 
25 }
26 
27 -(void)start:(UIButton*)sender
28 {
29     //http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0
30     
31     //获取URL
32     NSURL *url = [NSURL URLWithString:@"http://php.weather.sina.com.cn/xml.php"];
33     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
34     
35     //POST方式
36     [request setHTTPMethod:@"POST"];
37     NSString *param = @"city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0";
38     NSData *body = [param dataUsingEncoding:NSUTF8StringEncoding];
39     [request setHTTPBody:body];
40     
41     
42     //异步请求
43     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
44         
45         NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
46         
47         NSLog(@"数据:%@",content);
48         
49     }];
50     
51 }
52 
53 @end

IOS--POST异步请求

标签:

原文地址:http://www.cnblogs.com/yuge790615/p/5236513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!