AFNetworking常用代码...
2017-09-11 本文已影响39人
小苗晓雪
项目中经常用到的AFNetworking代码...
#import "ViewController.h"
#import "AFNetworking.h"
static NSString * urlString = @"http://192.168.20.1/login.php";
static NSString * uploadString = @"http://192.168.20.1/post/upload.php";
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//上传:
[[AFHTTPSessionManager manager] POST:uploadString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSURL * fileUrl = [[NSBundle mainBundle] URLForResource:@"xxx.png" withExtension:nil];
//上传表单操作
[formData appendPartWithFileURL:fileUrl name:@"userfile00" fileName:@"xxx.png" mimeType:@"image/png" error:nil];
} progress:^(NSProgress * _Nonnull uploadProgress) {
//上传的进度
NSLog(@"%@",@(uploadProgress.fractionCompleted));
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
- (void)upload1 {
NSURL * url = [NSURL URLWithString:@"http://192.168.1.34/images/huoyanshan.jpg"];
NSData * data = [NSData dataWithContentsOfURL:url];
UIImage * image = [UIImage imageWithData:data];
//上传图片urlsession
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:uploadString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSURL * fileUrl = [[NSBundle mainBundle] URLForResource:@"01.png" withExtension:nil];
//FileURL本地路径
//name 图片的key
//fileName 随意写
[formData appendPartWithFileURL:fileUrl name:@"userfile00" fileName:@"02.png" mimeType:@"image/png" error:nil];
NSData * data2 = UIImagePNGRepresentation(image);
//NSData二进制:
[formData appendPartWithFileData:data2 name:@"userfile00" fileName:@"03.png" mimeType:@"image/png"];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error)
{
NSLog(@"Error: %@", error);
}
else
{
NSLog(@"%@ %@", response, responseObject);
}
}] resume];
}
- (void)download {
//下载:
[[[[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]] downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.34/babeishang.mp3"]] progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
//获取沙盒documents路径:
NSURL * documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
//最终的路径:
NSLog(@"File downloaded to: %@", filePath);
}] resume];
}
- (void)post2 {
NSDictionary * params = @{@"username":@"haha",
@"password":@"123"};
NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:params error:nil];
//返回数据任务并resume开始执行网络请求:
[[[[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]] dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
NSLog(@"%@",responseObject);
}] resume];
}
- (void)post1
{
//post
NSDictionary * params = @{@"username":@"haha",
@"password":@"123"};
[[AFHTTPSessionManager manager] POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
- (void)get2
{
//get请求:
//获取一个AFHTTPSessionManager
//GET agr1 请求地址
//parameters 请求参数
//success 如果请求成功调用
//failure 如果请求失败调用
NSDictionary * params = @{@"username":@"haha",
@"password":@"123"};
[[AFHTTPSessionManager manager] GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
- (void)get1
{
//get请求:
NSString * urlStr = [urlString stringByAppendingString:@"?username=haha&password=123"];
NSURL *URL = [NSURL URLWithString:urlStr];
//response 响应头
//responseObject 获取到的数据
//error 网络连接的错误
[[[[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]] dataTaskWithRequest:[NSURLRequest requestWithURL:URL] completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error)
{
NSLog(@"Error: %@", error);
}
else
{
NSLog(@"%@ %@", response, responseObject);
}
}] resume];
}
@end