您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 網路請求:GET 與 POST 的同步、異步實作

編輯:iOS 開發綜合

在 iOS 早期,NSURLConnection 是發送 HTTP 請求的常見選擇。它把請求分成同步與異步兩種寫法,再搭配 GET 與 POST 兩種方法,就組成了四種基本組合。下面用四段程式碼逐一示範。

GET 同步

同步請求會在目前的執行緒上一直等到資料取回,因此適合放在背景佇列,切忌放在主線程以免卡住介面。

/*------------------------get同步-------------------------------------*/
- (IBAction)GET_TB:(id)sender
{
    //1.創建url
    NSURL *url = [NSURL URLWithString:@"https://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
    //2.創建一個請求對象
    NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:url];
    //請求方式
    [requst setHTTPMethod:@"get"];
    NSURLResponse *response = nil;
    NSError *err= nil;
    //3.建立連接
    NSData *data = [NSURLConnection sendSynchronousRequest:requst returningResponse:&response error:&err];
    NSLog(@"%@",data);
    NSLog(@"同步");
}

GET 異步

異步請求會立刻返回,結果透過 completionHandler 回呼,適合直接在使用者操作時觸發。

/*---------------------------get異步------------------------------------*/
- (IBAction)GET_YB:(id)sender
{
    //1.創建url
    NSURL *url = [NSURL URLWithString:@"https://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
    //2.創建請求對象
    NSURLRequest *requset = [NSURLRequest requestWithURL:url];
    //3.建立連接
    [NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%@",data);
    }];
    NSLog(@"異步");
}

POST 同步

POST 的差別在於要自備請求本文(body)。這裡把參數字串轉成 UTF-8 的 NSData,再設定到 request 上。

/*------------------------------post同步------------------------------------------*/
- (IBAction)POST_TB:(id)sender
{
    //1.創建URL
    NSURL *url = [NSURL URLWithString:@"https://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
    //2.創建請求對象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    //3.添加包體
    NSString *str =@"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215";
    //將字符串轉換成NSDATA格式
    NSData *dataBody = [str dataUsingEncoding:NSUTF8StringEncoding];
    //4.給請求設body
    [request setHTTPBody:dataBody];
    //5.創建連接
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSLog(@"%@",data);
    NSLog(@"POST同步");
}

POST 異步

/*------------------------------post異步------------------------------------------*/
- (IBAction)POST_YB:(id)sender
{
    //1.創建URL
    NSURL *url = [NSURL URLWithString:@"https://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
    //2.創建請求對象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    //3.給請求對象添加body
    NSString *bodyStr = @"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215";
    NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:dataBody];
    //4.創建連接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%@",data);
    }];
    NSLog(@"POST異步");
}

需要提醒的是,NSURLConnection 在後續的 iOS 版本中已被 NSURLSession 取代,新專案建議直接採用 NSURLSession;上面這套寫法適合作為理解請求流程的入門範例。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved