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");
}