IOS中get同步异步请求与post同步异步请求

demo 

//  Created by apple on 15/1/6.

//  Copyright (c) 2015 huweibin. All rights reserved.

//


#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 newcompletionHandler:^(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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值