新建工程代码如下:
//
// ViewController.m
// 异步连接参数解析
//
// Created by apple on 15/10/26.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1 url
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/demo.json"];
// 2. 建立请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 建立连接Connection,发送同步请求到服务器
/**
参数:
SynchronousRequest:发送同步请求
returningResponse:服务器响应的地址
error:错误信息的地址
*/
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"reponse= %@", response);
NSLog(@"%@——%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding], [NSThread currentThread]);
}
@end
运行结果如下: