iOS开发: AFNetworking网络请求:NSURLSession版
2015-10-29 08:24
615 查看
网络请求——NSURLSession版
1、NSURLSession的GET请求:
1>准备需要解析的网址字符串:
NSString *urlString = @"urlString";
2>将字符串转码,若字符串中包含汉字,需要转成 NSUTF8StringEncoding
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
3>字符串转化成URL
NSURL *url = [NSURL URLWithString:urlString];
4>创建request对象(可变的):
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
5>NSURLSession请求数据:
NSURLSession *session = [NSURLSession sharedSession];
6>创建负责执行请求任务的task对象:
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
//数据请求成功,可对解析完的数据JSON解析
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);
}
}];
7>task执行任务:
[task resume];
2、NSURLSession POST请求:
1>准备需要解析的字符串
NSString *urlString = @"urlString";
NSString *bodyString = @"bodyString";
2>字符串转码
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
3>创建URL对象
NSURL *url = [NSURL URLWithString:urlString];
4>创建request对象(可变的):
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
5>修改请求方式:
[request setHTTPMethod:@"POST"];
6>将bodyString转化成NSData类型:
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
7>设置request的bodyData:
[request setHTTPBody:bodyData];
8>设置NSURLSession对象:
NSURLSession *session = [NSURLSession sharedSession];
9>创建执行请求任务的task对象:
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);
}
}];
10> task执行任务
[task resume];
3.AFNetworking封装:
1>.下载最新版AFNetworking第三方类包,到入工程中;
2>.封装网络请求:
在Model(继承与NSObject).h文件中:
+
(void)getDataBystring:(NSString *)urlString BodyDic:(NSDictionary *)bodyDic WithDataBlock:(void (^)(id data))dataBlock;
说明:第一个参数是:urlString;第二个参数是:bodyString对应的字典;第三个参数是:请求数据的block格式
.m文件:
+ (void)getDataByString:(NSString *)urlString BodyDic:(NSDictionary *)bodyDic WithDataBlock:(void (^)(id))dataBlock{
1.:字符串的转码
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
2.创建管理者对象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
3.设置允许请求的类别
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", nil];
4.开始请求
if (!bodyDic) {
//如果bodyDic为空就执行get请求
[manager GET:urlString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
dataBlock(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"
1、NSURLSession的GET请求:
1>准备需要解析的网址字符串:
NSString *urlString = @"urlString";
2>将字符串转码,若字符串中包含汉字,需要转成 NSUTF8StringEncoding
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
3>字符串转化成URL
NSURL *url = [NSURL URLWithString:urlString];
4>创建request对象(可变的):
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
5>NSURLSession请求数据:
NSURLSession *session = [NSURLSession sharedSession];
6>创建负责执行请求任务的task对象:
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
//数据请求成功,可对解析完的数据JSON解析
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);
}
}];
7>task执行任务:
[task resume];
2、NSURLSession POST请求:
1>准备需要解析的字符串
NSString *urlString = @"urlString";
NSString *bodyString = @"bodyString";
2>字符串转码
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
3>创建URL对象
NSURL *url = [NSURL URLWithString:urlString];
4>创建request对象(可变的):
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
5>修改请求方式:
[request setHTTPMethod:@"POST"];
6>将bodyString转化成NSData类型:
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
7>设置request的bodyData:
[request setHTTPBody:bodyData];
8>设置NSURLSession对象:
NSURLSession *session = [NSURLSession sharedSession];
9>创建执行请求任务的task对象:
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);
}
}];
10> task执行任务
[task resume];
3.AFNetworking封装:
1>.下载最新版AFNetworking第三方类包,到入工程中;
2>.封装网络请求:
在Model(继承与NSObject).h文件中:
+
(void)getDataBystring:(NSString *)urlString BodyDic:(NSDictionary *)bodyDic WithDataBlock:(void (^)(id data))dataBlock;
说明:第一个参数是:urlString;第二个参数是:bodyString对应的字典;第三个参数是:请求数据的block格式
.m文件:
+ (void)getDataByString:(NSString *)urlString BodyDic:(NSDictionary *)bodyDic WithDataBlock:(void (^)(id))dataBlock{
1.:字符串的转码
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
2.创建管理者对象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
3.设置允许请求的类别
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", nil];
4.开始请求
if (!bodyDic) {
//如果bodyDic为空就执行get请求
[manager GET:urlString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
dataBlock(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"
相关文章推荐
- TCP、UDP
- 开发即时通信是选择UDP还是TCP协议
- 恶意软件感染MySQL服务器 成为全球DDoS僵尸网络一部分
- SDWebImage获取网络图片大小
- HttpServletRequestWrapper作用
- RTP与RTCP协议介绍
- HTTP报文内的HTTP信息之编码提升传输速率
- Java:网络编程之应用实例
- 关于JS中ajax的XMLHttpRequest对象的使用
- httpd+tomcat(3) -- mod_jk
- Java:网络编程之登陆服务器
- HTTP基本认证和摘要认证
- httpd+tomcat(2) -- proxy_ajp_module
- httpd+tomcat(1) -- proxy_http_module
- Java:网络编程值TCP的使用
- Java:网络编程之UDP的使用
- http://czpae86.iteye.com/blog/2174125
- Java:网络编程之IP、URL
- 网络下的数据传输之XML数据的解析
- Xcode7网络请求编译不通过的解决方案