IOS開發中使用AFNetworking請求網路資料

來源:互聯網
上載者:User

標籤:

1、把AFNetworking的檔案拖放到項目中(注意不同的版本方法不一樣,本曆程基於版本2013);

2、使用#import "AFNetworking.h"命令把AFNetworking.h=包含進來;

3、完整代碼:

 1 // 2 //  ViewController.m 3 //  網路請求 4 // 5 //  Created by mac on 16/5/12. 6 //  Copyright © 2016年 mzw. All rights reserved. 7 // 8  9 #import "ViewController.h"10 #import "AFNetworking.h"11 12 @interface ViewController ()13 14 @end15 16 @implementation ViewController17 18 - (void)viewDidLoad {19     [super viewDidLoad];20     21 //    1、先建立一個字串,把基url和介面url串連起來22     NSString *urlstring = [@"http://tl.bearapp.cn" stringByAppendingString:@"/api/index/zdmc.php"];23 //    2、使用上面建立的字串產生一個url24     NSURL *url = [NSURL URLWithString:urlstring];25 //    3、建立要求管理對象26     AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:url];//如果此處使用manager類方法初始化,就要在GET命令後面傳入url27 //    4、建立參數28     NSDictionary *dicc = [[NSDictionary alloc]initWithObjectsAndKeys:@"二道沙河",@"title", nil];29 //    5、擷取資源30     [manager GET:@"sdf" parameters:dicc success:^(AFHTTPRequestOperation *operation, id responseObject) {31         NSLog(@"%@",responseObject);//如果成功,則列印擷取的結果32     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {33         NSLog(@"%@",error);//如果失敗,則列印錯誤的資訊34     }];35 36 }37 38 @end

 

IOS開發中使用AFNetworking請求網路資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.