欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

IOS中g(shù)et同步異步請(qǐng)求與post同步異步請(qǐng)求 - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 技術(shù)分享 >

IOS中g(shù)et同步異步請(qǐng)求與post同步異步請(qǐng)求

發(fā)表時(shí)間:2021-1-4

發(fā)布人:葵宇科技

瀏覽次數(shù):71


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.準(zhǔn)備URL地址
    NSURL *url = [NSURL URLWithString:BASE_URL];
    
    //2.準(zhǔn)備請(qǐng)求對(duì)象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    //2.1設(shè)置請(qǐng)求方式
    [request setHTTPMethod:@"GET"];
    
    //3.準(zhǔn)備返回結(jié)果
    NSURLResponse *response = nil;
    NSError *error = nil;
    
    //4.創(chuàng)建鏈接對(duì)象,并發(fā)送請(qǐng)求,并獲取結(jié)果(需要的數(shù)據(jù))
    NSData *data =http://www.sjsjw.com/100/000571MYM030394/ [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    //5.打印獲取到的一些信息
    NSLog(@"結(jié)果類型:%@",response.MIMEType);
    NSLog(@"請(qǐng)求的網(wǎng)址:%@",response.URL);
    NSLog(@"結(jié)果長(zhǎng)度:%lld",response.expectedContentLength);
    NSLog(@"請(qǐng)求到的結(jié)果:%@",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.準(zhǔn)備url地址
    NSURL *url = [NSURL URLWithString:_BASE_URL];
    //2.創(chuàng)建請(qǐng)求對(duì)象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //3.創(chuàng)建鏈接對(duì)象,發(fā)送請(qǐng)求
    [NSURLConnection connectionWithRequest:request delegate:self];
    
}
#pragma mark - POST同步
- (IBAction)postSyncButtonAction:(UIButton *)sender
{
    //1.準(zhǔn)備網(wǎng)址
    NSURL *url = [NSURL URLWithString:_BASE_URL];
    
    //2.準(zhǔn)備請(qǐng)求對(duì)象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    //2.1設(shè)置請(qǐng)求方式
    [request setHTTPMethod:@"POST"];
    
    //2.2設(shè)置請(qǐng)求參數(shù)
#warning 設(shè)置請(qǐng)求參數(shù),需要的是NSData類型
    NSData *param = [_BASE_URL1_PARAM dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:param];
    
    //3.創(chuàng)建鏈接對(duì)象,并發(fā)送請(qǐng)求,獲取結(jié)果
    NSData *data = http://www.sjsjw.com/100/000571MYM030394/[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.準(zhǔn)備地址
    NSURL *url = [NSURL URLWithString:_BASE_URL];
    //2.創(chuàng)建請(qǐng)求對(duì)象,并設(shè)置請(qǐng)求方法和參數(shù)
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[_BASE_URL1_PARAM dataUsingEncoding:NSUTF8StringEncoding]];
    
    //3.創(chuàng)建鏈接對(duì)象,發(fā)送請(qǐng)求,在block內(nèi)部完成分析
    [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.回到主線程,進(jìn)行更新頁(yè)面
        dispatch_sync(dispatch_get_main_queue(), ^{
            weakSelf.textView.text = [NSString stringWithFormat:@"%@",dict];
        });
        
    }];
    
    
    
}
#pragma mark - 清除
- (IBAction)clearButtonAction:(UIButton *)sender
{
    _textView.text = nil;
}
#pragma mark - 實(shí)現(xiàn)協(xié)議方法
#pragma mark 開始接收請(qǐng)求結(jié)果
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //初始化
    self.mutableData = http://www.sjsjw.com/100/000571MYM030394/[NSMutableData data];
}
#pragma mark - 接收數(shù)據(jù)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //拼接接收到的數(shù)據(jù)
    [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 - 接收錯(cuò)誤
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
}- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

相關(guān)案例查看更多