IOS--POST非同步請求

來源:互聯網
上載者:User

標籤:

TestController.m

 1 #import "TestController.h" 2  3 @interface TestController() 4  5 @property(nonatomic,strong)UIButton *button; 6  7 @end 8  9 @implementation TestController10 11 - (void)viewDidLoad12 {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*)sender28 {29     //http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=030     31     //擷取URL32     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非同步請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.