IOS中get同步異步請求與post同步異步請求 - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 技術分享 >

IOS中get同步異步請求與post同步異步請求

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

發布人(rén):融晨科技

浏覽次數: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.準備URL地(dì / de)址
    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.創建鏈接對象,并發送請求,并獲取結果(需要(yào / yāo)的(de)數據)
    NSData *data =http://www.sjsjw.com/100/000571MYM030394/ [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    //5.打印獲取到(dào)的(de)一些信息
    NSLog(@"結果類型:%@",response.MIMEType);
    NSLog(@"請求的(de)網址:%@",response.URL);
    NSLog(@"結果長度:%lld",response.expectedContentLength);
    NSLog(@"請求到(dào)的(de)結果:%@",data);
    
    //6.解析文件
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
    //7.顯示在(zài)textView裏
    self.textView.text = [NSString stringWithFormat:@"%@",dict];
    
}
#pragma mark - get異步
- (IBAction)getAsyncButtonAction:(UIButton *)sender
{
    //1.準備url地(dì / de)址
    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 設置請求參數,需要(yào / yāo)的(de)是(shì)NSData類型
    NSData *param = [_BASE_URL1_PARAM dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:param];
    
    //3.創建鏈接對象,并發送請求,獲取結果
    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.準備地(dì / de)址
    NSURL *url = [NSURL URLWithString:_BASE_URL];
    //2.創建請求對象,并設置請求方法和(hé / huò)參數
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[_BASE_URL1_PARAM dataUsingEncoding:NSUTF8StringEncoding]];
    
    //3.創建鏈接對象,發送請求,在(zài)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.回到(dào)主線程,進行更新頁面
        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 = http://www.sjsjw.com/100/000571MYM030394/[NSMutableData data];
}
#pragma mark - 接收數據
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //拼接接收到(dào)的(de)數據
    [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

相關案例查看更多