IOS 开发 之 网络编程

来源:互联网 发布:软件流量是什么 编辑:程序博客网 时间:2024/05/14 18:36

如果此文帮助了您,请点击喜欢或评论,如果您喜欢我的文章请关注我,您的支持永远都是我前行的动力.

注意

1.发送请求
url : = 网络协议(http://今后可能是https) + 文件路径? + 参数1 & 参数2 …
2.接收响应
3.创建连接对象传输数据

xcode7 之后若想要使用http,需要修改info.plist文件
NSAppTransportSecucurity NSDictionary
NSAllowsArbitraryLoads BOOL YES;

第一种get 同步

1.创建URL对象
NSURL *url = [NSURL URLWithString:BASE_URL];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
2.1 创建请求方式(默认是get,这一步可以不写)
[request setHTTPMethod:@"get"];
3.创建响应对象(有时会出错)

NSURLResponse *response = nil;
NSError *error = nil;

4.创建连接对象
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
开辟空间

self.dataArray = [NSMutableArray arrayWithCapacity:5];
for (NSDictionary *dict in dic[@"news"]) {
News *news = [News new];
[news setValuesForKeysWithDictionary:dict];
[_dataArray addObject:news];
}
for (News *news in _dataArray) {
NSLog(@"%@",news);
}

第二种 post 同步

注:浏览器只能模拟get

1.创建url对象
NSURL *url = [NSURL URLWithString:URL_PORT1];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
2.1(必须写) 创建请求方式
[request setHTTPMethod:@"post"];
3.设置请求参数

NSData *tempData = [URL_PORT2 dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:tempData];

4.创建响应对象
NSURLResponse *response = nil;
NSError *error = nil;

5.创建连接对象
NSData *data =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
6.解析

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",dic);
self.dataArray = [NSMutableArray arrayWithCapacity:5];
for (NSDictionary *dicq in dic[@"news"]) {
News *news = [News new];
[news setValuesForKeysWithDictionary:dicq];
[_dataArray addObject:news];
}
for (News *ee in _dataArray) {
NSLog(@"%@",ee);
}

第三种 GET异步 – BLOCK

  1. 创建URL对象
    NSURL *url = [NSURL URLWithString:BASE_URL];
    2.创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    3.创建连接对象

[NSOperationQueue mainQueue]主队列,多线程内容.
__weak typeof (self)temp = self;

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"get异步---%d---",[[NSThread currentThread]isMainThread]);
temp.dataArray = [NSMutableArray arrayWithCapacity:5];
}];
NSLog(@"get异步---%d---",[[NSThread currentThread]isMainThread]);
}

第四种 POST异步 – BLOCK

1.创建url对象
NSURL *url = [NSURL URLWithString:URL_PORT1];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
2.1(必须写) 创建请求方式
[request setHTTPMethod:@"post"];
3.设置请求参数

NSData *tempData = [URL_PORT2 dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:tempData];

4.设置创建连接对象
__weak typeof (self)ttemp = self;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (nil != data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}
ttemp.dataArray = [NSMutableArray arrayWithCapacity:5];
}];

第五种 get异步 – 代理”

1.创建URL对象
NSURL *url = [NSURL URLWithString:BASE_URL];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
3.创建连接对象,并实现代理
NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];
4.开始执行
[connection start];

get异步代理

NSURLConnectionDataDelegate

1.接收到服务器的响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
准备数据接收

self.tempData = [NSMutableData data];
}

2.接收请求的数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
将每次新接收到的数据拼接到原有数据包的后面;

[_tempData appendData:data];
}

3.加载完毕,开始解析
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_tempData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}

打印失败信息

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"文件链接出现了error:%@",error);
}

第六种 post异步 – 代理

NSURL *url = [NSURL URLWithString:URL_PORT1];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"post"];
NSData *tempData = [URL_PORT2 dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:tempData];
NSURLConnection *connertion = [NSURLConnection connectionWithRequest:request delegate:self];
[connertion start];

1.接收到服务器的响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
准备数据接收

self.tempData = [NSMutableData data];
}

2.接收请求的数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
将每次新接收到的数据拼接到原有数据包的后面;

[_tempData appendData:data];
}

3.加载完毕,开始解析
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_tempData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}

打印失败信息

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"文件链接出现了error:%@",error);
}

post异步session

1.创建URL对象
NSURL *url = [NSURL URLWithString:URL_PORT1];
2.创建请求对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
2.1
[request setHTTPMethod:@"post"];
参数转化
NSData *tempData = [URL_PORT2 dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:tempData];
3.建立会话
NSURLSession *session = [NSURLSession sharedSession];
session支持三种类型的任务
NSURLSessionDataTask;//(加载数据)
NSURLSessionDownloadTask;//(下载)
NSURLSessionUploadTask;//(上传)

//判断是否是子线程 1是子线程,0不是主线程

NSLog(@"%d",[[NSThread currentThread]isMainThread]);
__weak typeof (self)temp = self;
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; //解析
NSLog(@"%@",dic);
NSLog(@"---%d---",[[NSThread currentThread]isMainThread]);

回到主线程(刷新数据)
dispatch_async(dispatch_get_main_queue(), ^{
相当于tableView的刷新数据
[temp.tableView reloadData];
});
}];

4.启动任务
[task resume];
}

get异步session

1.创建url对象
NSURL *url = [NSURL URLWithString:BASE_URL];
2.创建session对象
NSURLSession *session = [NSURLSession sharedSession];
3.创建task (该方法内部做了处理,默认使用get,直接传递Url即可);
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

dispatch_async(dispatch_get_main_queue(), ^{
});

[task resume];
}

转载请注明出处.

0 0
原创粉丝点击