简单的 同步 异步 请求

 #import "ViewController.h"


@interface ViewController ()
@property(nonatomic,strong)UITextView *textView; @property(nonatomic,copy)NSString *BASE_URL; @property(nonatomic,copy)NSString *BASE_URL1_PARAM; @property(nonatomic,strong)NSMutableData *mutableData; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } #pragma mark - get同步 - (IBAction)getSyncButtonAction:(UIButton *)sender { NSString * BASE_URL= @"www.baidu.com"; //1.准备URL地址 NSURL *url = [NSURL URLWithString:BASE_URL];
//2.准备请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2.1设置请求方式 [request setHTTPMethod:@"GET"];
//3.准备返回结果 NSURLResponse *response = nil; NSError *error = nil;
//4.创建链接对象,并发送请求,并获取结果(需要的数据) NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//5.打印获取到的一些信息 NSLog(@"结果类型:%@",response.MIMEType); NSLog(@"请求的网址:%@",response.URL); NSLog(@"结果长度:%lld",response.expectedContentLength); NSLog(@"请求到的结果:%@",data);
//6.解析文件 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; //7.显示在textView里 self.textView.text = [NSString stringWithFormat:@"%@",dict]; }
#pragma mark - get异步 - (IBAction)getAsyncButtonAction:(UIButton *)sender { //1.准备url地址 NSURL *url = [NSURL URLWithString:_BASE_URL]; //2.创建请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //3.创建链接对象,发送请求 [NSURLConnection connectionWithRequest:request delegate:self]; } #pragma mark - POST同步 - (IBAction)postSyncButtonAction:(UIButton *)sender { //1.准备网址 NSURL *url = [NSURL URLWithString:_BASE_URL]; //2.准备请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //2.1设置请求方式 [request setHTTPMethod:@"POST"]; //2.2设置请求参数 #warning 设置请求参数,需要的是NSData类型 NSData *param = [_BASE_URL1_PARAM dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:param];
//3.创建链接对象,并发送请求,获取结果 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //4.解析 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; //5.显示 self.textView.text = [NSString stringWithFormat:@"%@",dict]; } #pragma mark - POST异步 - (IBAction)postAsyncButtonAction:(UIButton *)sender { __block ViewController *weakSelf = self;
//1.准备地址 NSURL *url = [NSURL URLWithString:_BASE_URL]; //2.创建请求对象,并设置请求方法和参数 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[_BASE_URL1_PARAM dataUsingEncoding:NSUTF8StringEncoding]]; //3.创建链接对象,发送请求,在block内部完成分析 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

//NSLog(@"%@",data); //4.解析 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; //5.回到主线程,进行更新页面 dispatch_sync(dispatch_get_main_queue(), ^{ weakSelf.textView.text = [NSString stringWithFormat:@"%@",dict]; }); }]; } #pragma mark - 清除 - (IBAction)clearButtonAction:(UIButton *)sender { _textView.text = nil; } #pragma mark - 实现协议方法 #pragma mark 开始接收请求结果 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { //初始化 self.mutableData = [NSMutableData data]; } #pragma mark - 接收数据 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { //拼接接收到的数据 [self.mutableData appendData:data]; } #pragma makr - 接收完毕 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { //解析 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_mutableData options:NSJSONReadingAllowFragments error:nil]; _textView.text = [NSString stringWithFormat:@"%@",dict]; } #pragma mark - 接收错误 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
原文地址:https://www.cnblogs.com/yeng/p/5767794.html