iOS--AFNetworking3.0的使用
从github下载AFNetworking3.0,把AFNetworking.h 和 UIKit+AFNetworking.h导入工程
(下载地址:https://github.com/AFNetworking/AFNetworking.git)
注意!!
AFNetworking不支持text/html
遇到请求头到错误时在AFURLResponseSerialization.m里修改
1 //把@"text/html" 添加到下面代码里就行, 2 self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
或者在需要的地方写代码
1 //设置请求头contenttypes信息 2 manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
//初始化 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
如果后台接口是二进制格式
//默认的 manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.responseSerializer = [AFHTTPResponseSerializer serializer];
如果后台接口是JSON格式
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
GET
Request
[manager GET:@"example" parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) { //这里可以用来显示下载进度 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { //成功 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { //失败 }];
POST
Request
//post请求参数一般用字典,键要与请求url的参数名一致。 [manager POST:@"example" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) { //进度 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { //成功 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { //失败 }];
Shared Network Reachability
//监听网络 /* AFNetworkReachabilityStatusUnknown = -1, AFNetworkReachabilityStatusNotReachable = 0, AFNetworkReachabilityStatusReachableViaWWAN = 1, AFNetworkReachabilityStatusReachableViaWiFi = 2, NSString * AFStringFromNetworkReachabilityStatus(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusNotReachable: return NSLocalizedStringFromTable(@"Not Reachable", @"AFNetworking", nil); case AFNetworkReachabilityStatusReachableViaWWAN: return NSLocalizedStringFromTable(@"Reachable via WWAN", @"AFNetworking", nil); case AFNetworkReachabilityStatusReachableViaWiFi: return NSLocalizedStringFromTable(@"Reachable via WiFi", @"AFNetworking", nil); case AFNetworkReachabilityStatusUnknown: default: return NSLocalizedStringFromTable(@"Unknown", @"AFNetworking", nil); } } */ [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status))
}];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
//单纯判断有无网络可以用isReachable属性 if ([[AFNetworkReachabilityManager manager] isReachable] == NO) { //do something }
上传图片
图片上传的时候要问后台只能单张上传或者多张上传.
//单张上传 [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { //创建待上传图片的名称,为了不重名,一般使用当前时间+图片类型(eg .png)为名称 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; //服务器保存上传图片的路径,这个路径后台给的话用后台的,没给就自己创建(此处没给) NSString *name = [formatter stringFromDate:[NSDate date]]; //后缀 .png .jpg NSString *fileName = [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".jpg"]; /* *UIImageJPEGRepresentation(image,1.0) //待压缩比例 *UIImagePNGRepresentation(image) *这是iOS自带的将UIImage对象转成NSData */ NSData *data = UIImageJPEGRepresentation(image,1.0); //开始上传 [formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpg"]; } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { //根据responseObject获取后台返回的内容 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
/* * 多张上传和单张上传的区别: * 图片名要按时间+数组中图片的index+.jpg * 服务器保存图片的路径按时间+数组中图片的index */ //多张上传 //图片上传 for (int i = 0; i < imageArr.count; i ++) { [manager POST:@"http://112.54.80.235:50406/IndustryPioneer.svc/uploadimageIos" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { //创建待上传图片的名称,为了不重名,一般使用当前时间+图片类型(eg .png)为名称 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; //服务器保存上传图片的路径,这个路径后台给的话用后台的,没给就自己创建(此处没给) NSString *name = [NSString stringWithFormat:@"%@%d",[formatter stringFromDate:[NSDate date]],i]; //后缀 .png .jpg NSString *fileName = [[NSString stringWithFormat:@"%@%d",[formatter stringFromDate:[NSDate date]],i] stringByAppendingString:@".jpg"]; /* *UIImageJPEGRepresentation(image,1.0) //待压缩比例 *UIImagePNGRepresentation(image) *这是iOS自带的将UIImage对象转成NSData */ NSData *data = UIImageJPEGRepresentation([imageArr objectAtIndex :i],1.0); //开始上传 [formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpg"]; } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { //根据responseObject获取后台返回的内容 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }]; }