最近一直很忙也沒(méi)有時(shí)間寫博客嗡害,今天正好有點(diǎn)空閑時(shí)間,所以就對(duì) AFNetworking 做了一些簡(jiǎn)單的總結(jié)畦攘,特地記錄下來(lái)霸妹,以便遇到的朋友們看看!V骸叹螟!
先說(shuō)下這篇文章的大概內(nèi)容,以便朋友們能夠快速找到自己需要的台盯。這篇文章主要介紹了三種網(wǎng)絡(luò)請(qǐng)求的渠道罢绽,第一種是通過(guò)蘋果自帶的 NSURLSession 請(qǐng)求網(wǎng)絡(luò);第二種是通過(guò) AFNetworking2.x 請(qǐng)求網(wǎng)絡(luò)静盅;最后一種是通過(guò) AFNetworking3.0 請(qǐng)求網(wǎng)絡(luò)良价。下邊是這三種網(wǎng)絡(luò)請(qǐng)求渠道的 demo.
一、蘋果自帶的請(qǐng)求網(wǎng)絡(luò)方法:
這里介紹了三種蒿叠,GET 請(qǐng)求明垢、POST 請(qǐng)求、代理方式請(qǐng)求市咽。
1.GET 請(qǐng)求:
// 獲得 NSURLSession 對(duì)象
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
// 創(chuàng)建任務(wù)
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@" "]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"data ~~~ %@ ~~~ response ~~~ %@ ~~~ error~~~ %@", data, response , error);
}];
// 啟動(dòng)任務(wù)
[dataTask resume];
2.POST 請(qǐng)求:
// 獲得NSURLSession對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
// 創(chuàng)建請(qǐng)求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@" "]];
// 設(shè)置請(qǐng)求方法
request.HTTPMethod = @"POST";
// 設(shè)置請(qǐng)求體
request.HTTPBody = [@" " dataUsingEncoding:NSUTF8StringEncoding];
// 創(chuàng)建任務(wù)
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"data ~~~ %@ ~~~ response ~~~ %@ ~~~ error~~~ %@", data, response , error);
}];
// 啟動(dòng)任務(wù)
[task resume];
3.代理:NSURLSessionDataDelegate
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@" "]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
// 如果創(chuàng)建task時(shí)痊银,有block,代理會(huì)失效
NSURLSessionDataTask *task = [session dataTaskWithRequest:request];
[task resume];
}
#pragma mark - NSURLSessionDataDelegate
#pragma mark - 接收到響應(yīng)頭調(diào)用的協(xié)議方法
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
NSHTTPURLResponse *httpRes = (NSHTTPURLResponse *)response;
NSLog(@"%@",httpRes.allHeaderFields);
//通過(guò)設(shè)置 completionHandler, Block 可以設(shè)置后面的響應(yīng)體中的數(shù)據(jù)是否繼續(xù)發(fā)送,默認(rèn)是不發(fā)送
completionHandler(NSURLSessionResponseAllow);
}
#pragma mark - 數(shù)據(jù)請(qǐng)求完成后施绎,調(diào)用的協(xié)議方法溯革,data是響應(yīng)體中的數(shù)據(jù)
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data {
NSLog(@"%@",data);
}
#pragma mark - 不管請(qǐng)求成功還是失敗,最終都會(huì)調(diào)用此方法
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error) {
NSLog(@"請(qǐng)求失敗");
} else {
NSLog(@"請(qǐng)求成功");
}
}
二谷醉、AFNetworking2.x
1.GET 請(qǐng)求
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager GET:@" " parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"請(qǐng)求成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"請(qǐng)求失敗");
}];
2.POST 請(qǐng)求
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager POST:@" " parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"請(qǐng)求成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"請(qǐng)求失敗");
}];
三致稀、AFNetworking3.0
1.GET 請(qǐng)求
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
// 解析服務(wù)器返回的JSON數(shù)據(jù)
// [AFJSONResponseSerializer serializer];
// 解析服務(wù)器返回的XML數(shù)據(jù)
// [AFXMLParserResponseSerializer serializer];
// 參數(shù)1: 請(qǐng)求的網(wǎng)址
// 參數(shù)2: 參數(shù)
// 參數(shù)3: 當(dāng)前的進(jìn)度
// 參數(shù)4: 請(qǐng)求成功
// 參數(shù)5: 請(qǐng)求失敗
[sessionManager GET:@" " parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"進(jìn)度");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"請(qǐng)求成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"請(qǐng)求失敗");
}];
2.POST 請(qǐng)求
// 創(chuàng)建管理者對(duì)象
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
// 設(shè)置請(qǐng)求參數(shù)
NSString *urlStr = @" ";
// 需要設(shè)置 body 體
NSDictionary *dic = @{@" " : @" ",@" " : @" "};
[sessionManager POST:urlStr parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"進(jìn)度");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"請(qǐng)求成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"請(qǐng)求失敗");
}];
上面的一些代碼都是基礎(chǔ)性代碼,因此也沒(méi)怎么做注釋孤紧,如果有疑問(wèn)的話可以私信交流...另外代碼有什么問(wèn)題的話也希望大家能夠指出豺裆!謝謝大家!