ftp:(文件传输协议)
http:(超文本传输协议)
https:(安全超文本传输协议)
file:(本地文件协议)
Xcode7设置网络:
打断点:在输出框 输入:po self.array 会打印出里面的对象
或者:
<key>NSAppTransportSecurity</key><dict>
<key>NSAllowsArbitraryLoads</key>
<true/></dict>
1. GET:
1.1 GET同步:
sendSynchronousRequest:request returningResponse:nil error:nil
1> 获取网址
2> 建立连接,请求数据(NSMutableURLRequest *request),获取数据:NSURLConnection sendSynchronousRequest
3> 对数据进行解析
- (IBAction)getOne:(id)sender {
// 初始化数组属性
self.arrayAllNews = [[NSMutableArray alloc] init];
// 1. 网址
NSString *string = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
// url
NSURL *url = [NSURL URLWithString:string];
// 2. 请求对象 可变的 传了地址(将url给一个请求对象)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 3. 建立连接 请求数据
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// 4. 解析:在解析之前判断data是否拿到了数据
if (data != nil) {
// 拿到最外层的字典
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// 用model处理数据
// 外层一个大的news 里面是一堆字典
for (NSDictionary *diction in dic[@"news"]) {
NewsModel *model = [[NewsModel alloc] init];
[model setValuesForKeysWithDictionary:diction];
[self.arrayAllNews addObject:model];
}
}
// 遍历打印:
for (NewsModel *model in self.arrayAllNews) {
NSLog(@"%@", model);
}
}
1.2 GET session:
dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
首先:[NSURLSession sharedSession]
设置URL
请求对象
建立连接 请求数据
- (IBAction)session:(id)sender {
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// 后面的内容:将字典转为model存储 和之前的一样
}
}];
// 必须调用这个方法 才会有数据
[task resume];
}
1.3 GET 异步:
sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
有block
- (IBAction)buttonGetTwo:(id)sender {
self.arrayAllNews1 = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
// 创建请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 默认就是GET形式 所以可以不用写
[request setHTTPMethod:@"GET"];
// 建立连接
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (data != nil) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
for (NSDictionary *diction in dic[@"news"]) {
NewsModel *model = [[NewsModel alloc] init];
[model setValuesForKeysWithDictionary:diction];
[self.arrayAllNews1 addObject:model];
}
}
for (NewsModel *model in self.arrayAllNews1) {
NSLog(@"%@", model);
}
}];
}
2. POST:
2.1 POST异步:Block形式
设置URL
单独封装数据
设置body
建立连接 请求数据
- (IBAction)postBlock:(id)sender {
// 1. 初始化
self.arrayAllNews2 = [[NSMutableArray alloc] init];
// 2. 设置URL
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 3. 制作body(将数据单独拿出来 封装)
NSString *param = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData *data = [param dataUsingEncoding:NSUTF8StringEncoding];
// 4. 设置body
[request setHTTPBody:data];
[request setHTTPMethod:@"POST"];// 这个要写
// connection建立连接 请求数据
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",dic);
for (NSDictionary *diction in dic[@"news"]) {
NewsModel *model = [[NewsModel alloc] init];
[model setValuesForKeysWithDictionary:diction];
[self.arrayAllNews2 addObject:model];
}
}
for (NewsModel *model in self.arrayAllNews2) {
NSLog(@"%@", model);
}
}];
}
2.2 POST代理:
遵守协议:<NSURLConnectionDataDelegate>
#pragma mark POST异步代理形式
- (IBAction)postDelegate:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];// 设置请求形式
NSString *string = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
// 建立连接 请求数据
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// 启动请求 才会调用代理方法
[connection start];
}
#pragma mark POST代理形式的代理方法:
#pragma 接收响应
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"接收到响应了");
self.data = [[NSMutableData alloc] init];
}
#pragma 接收数据(拼接)
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.data appendData:data];
}
#pragma 结束传输数据(然后进行解析)
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
// 数据解析:JSON
// 返回的是id类型 (根据数据判断返回的具体类型)
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:nil];
NSLog(@"%@",dic);
}