您的位置:首页 > 理论基础 > 计算机网络

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(@"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: