pragma mark 总结:常用的请求方式有两种, 一个是GET, 一个是POST, 他两本质上没有任何区别, 只是post在请求的时候需要添加一个body, 同步和异步: 都使用异步的方式进行加载, 加载过程中还可以操作其他的功能, 不会出现卡死的情况, 从同步演化出异步, 请求分为三步: 1. 创建URL 2. 创建请求request, 3. 建立连接, 完成数据请求, iOS9.0之后, NSURLConnection用的越来越局限, NSURLSession未来更重要
NSString *strURL = @"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295";
NSString *strURLEncode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strURLEncode];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", dic);
NSLog(@"*******************%@", response);
NSString *urlStr = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"post"];
NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:dataBody];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableArray *arr = dic[@"news"];
for (NSDictionary *dic in arr) {
NSLog(@"\n");
NSLog(@"\n");
NSLog(@"**********************************************%@", dic[@"summary"]);
NSLog(@"\n");
NSLog(@"\n");
}