iOS_GET_網路請求

來源:互聯網
上載者:User

iOS_GET_網路請求

同步的 get 請求

#pragma mark - 同步的 get 請求- (IBAction)GETSynButtonDidClicked:(UIButton *)sender {    // 1、網址裡面必須寫 http://    NSString *urlString = @http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213;    // 2、如果網址有漢字需要轉換(沒有漢字也可以寫)    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    // 3、根據字串建立 url(統一資源定位器)    NSURL *url = [NSURL URLWithString:urlString];    // 4、根據 url 建立 request 請求類的對象    NSURLRequest *request = [NSURLRequest requestWithURL:url];    // 5、開始去請求網路、資料(同步) 返回data    NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];    // 6、系統內建json解析    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:receiveData options:(NSJSONReadingMutableContainers) error:nil];    NSArray *array = dict[@news];    self.newsArray = [NSMutableArray array];    for (NSDictionary *smallDict in array) {        NewsModal *modal = [[NewsModal alloc] init];        [modal setValuesForKeysWithDictionary:smallDict];        [self.newsArray addObject:modal];    }    for (NewsModal *modal in self.newsArray) {        NSLog(@%@, modal.title);    }}

非同步 get 請求

#pragma mark - 非同步 get 請求- (IBAction)GETAsyButtonDidClicked:(UIButton *)sender {    // 1、拼接 urlString,網址裡面必須寫 http://    NSString *urlString = @http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213;    // 2、根據字串建立 URL(統一資源定位器)    NSURL *url = [NSURL URLWithString:urlString];    // 3、根據 url 建立 request 請求類的對象    NSURLRequest *request = [NSURLRequest requestWithURL:url];    // 4、開始去請求網路、資料(同步) 返回data    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        // 5、系統內建json解析        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];        NSArray *array = dict[@news];        self.newsArray = [NSMutableArray array];        for (NSDictionary *smallDict in array) {            NewsModal *modal = [[NewsModal alloc] init];            [modal setValuesForKeysWithDictionary:smallDict];            [self.newsArray addObject:modal];        }        for (NewsModal *modal in self.newsArray) {            NSLog(@%@, modal.title);        }    }];}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.