1端三、get請求
2锯茄、post請求
3搔驼、文件下載
4谈火、文件上傳
AFNetworking3.0是對NSURLSession的包裝,很多細(xì)節(jié)都封裝起來了舌涨,不用擔(dān)心
- get請求
- (void)get {
// 創(chuàng)建會話管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *paramDict = @{
@"username" : @"520it",
@"pwd" : @"520it",
@"type" : @"JSON"
};
/**
* 發(fā)送get請求
* 第一個(gè)參數(shù):請求路徑(不包含參數(shù))
* 第二個(gè)參數(shù):字典(發(fā)送給服務(wù)器的數(shù)據(jù))
* 第三個(gè)參數(shù):progress 進(jìn)度回調(diào)
* 第四個(gè)參數(shù):success 成功回調(diào)
* 第五個(gè)參數(shù):failure
*/
[manager GET:@"http://120.25.226.186:32812/login" parameters:paramDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
- post請求
- (void)post {
// 創(chuàng)建會話管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *paramDict = @{
@"username" : @"520it",
@"pwd" : @"520it",
@"type" : @"JSON"
};
/**
* 發(fā)送post請求
* 第一個(gè)參數(shù):請求路徑(不包含參數(shù))
* 第二個(gè)參數(shù):字典(發(fā)送給服務(wù)器的數(shù)據(jù))
* 第三個(gè)參數(shù):progress 進(jìn)度回調(diào)
* 第四個(gè)參數(shù):success 成功回調(diào)
* 第五個(gè)參數(shù):failure
*/
[manager POST:@"http://120.25.226.186:32812/login" parameters:paramDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
- 下載糯耍。 這里有一些注意的地方
- manager有返回值,需要接收之后開啟任務(wù)囊嘉。
- destination有返回值
- (void)dowLoad {
// 創(chuàng)建會話管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_04.mp4"]];
/**
* 文件下載
* 參數(shù)1:請求對象
* 參數(shù)2:progress 進(jìn)度回調(diào) completedUnitCount已完成 / totalUnitCount總的
* 參數(shù)3:destination 回調(diào)(目標(biāo)位置)
有返回值
targetPath 臨時(shí)文件路徑
response 響應(yīng)頭信息
* 參數(shù)4:completionHandler 下載完成后的回調(diào)
filePath 文件路徑
*/
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"%f", 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
return [NSURL fileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
}];
// 執(zhí)行task
[download resume];
}
- 上傳文件
- (void)upLoad {
// 創(chuàng)建會話管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 發(fā)送post請求上傳文件
/**
* 參數(shù)1:請求路徑
* 參數(shù)2:字典(非文件參數(shù))
* 參數(shù)3:constructingBodyWithBlock 處理要上傳的文件數(shù)據(jù)
* 參數(shù)4:進(jìn)度回調(diào)
* 參數(shù)5:上傳成功回調(diào) responseObject響應(yīng)體信息
*/
[manager POST:@"http://120.25.226.186:32812/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
// UIImage *image = [UIImage imageNamed:@"xmg"];
// NSData *imageData = UIImagePNGRepresentation(image);
// 使用formData來拼接數(shù)據(jù)
/**
* 參數(shù)1:二進(jìn)制數(shù)據(jù)温技,要上傳的文件參數(shù)
* 參數(shù)2:服務(wù)器規(guī)定的
* 參數(shù)3:該文件上傳到服務(wù)器以什么名稱保存
*/
// [formData appendPartWithFileData:imageData name:@"file" fileName:@"xxx.png" mimeType:@"image/png"];
// [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/mac2/Desktop/xmg.png"] name:@"file" fileName:@"xxx.png" mimeType:@"image/png" error:nil];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/mac2/Desktop/xmg.png"] name:@"file" error:nil];
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%f", 1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"上傳成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"上傳失敗");
}];
}
- AFN判斷網(wǎng)絡(luò)狀態(tài)
AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
/*
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 2,
*/
switch (status) {
case AFNetworkReachabilityStatusUnknown:
MH_LOG(@"網(wǎng)絡(luò)狀態(tài)未知");
break;
case AFNetworkReachabilityStatusNotReachable:
MH_LOG(@"沒有網(wǎng)絡(luò)");
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotReachable" object:nil];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
MH_LOG(@"3G|4G蜂窩移動網(wǎng)絡(luò)");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
MH_LOG(@"WIFI網(wǎng)絡(luò)");
break;
default:
break;
}
}];
開始監(jiān)聽
[manager startMonitoring];