AFN3.0API调用
来源:互联网 发布:网络弊大于利反方辩词 编辑:程序博客网 时间:2024/06/05 07:46
关于AFNetworking 3.0NSURLConnection的API已废弃
AFNetworking 1.0 是建立在 NSURLConnection基础上的,AFNetworking 2.0 开始使用基于NSURLConnection *API基础功能,同时也有基于新的NSURLSession* API 的功能实现。AFNetworking 3.0现在是专门建立在 NSURLSession 顶层的,这降低了维护负担,同时允许支持苹果为 NSURLSession 提供的任何额外的增强的特性。在Xcode 7,NSURLConnection API 已经被苹果官方弃用。然而API函数将继续使用不会受影响,只不过再也不会添加新的功能了,苹果建议所有基于网络的功能在未来都能使用NSURLSession。
3.0 被移除的类有
转而替代的是
干货来了:
1----->基本的post,get请求--->.h
基本的post,get请求实现---->.m
2----->上传图片
.h
.m
3---->视频上传实现
.h
.m
4----->文件下载
.h
.m
5---->取消指定的url请求
.h
.m
参考网址:http://code4app.com/thread-8424-1-1.html
3.0 被移除的类有
• AFURLConnectionOperation • AFHTTPRequestOperation • AFHTTPRequestOperationManager
转而替代的是
• AFURLSessionManager • AFHTTPSessionManager
主要实现的功能下面的方法也是基于NSURLSession进行封装的 所有的请求方法都在NetWorkManager.m进行统一设置,并且全部为类方法(调用方便)
1.文件下载 2.多图压缩上传 3.视频上传(文件上传,音频上传类似) 4.取消所有的网络请求 5.取消指定的网络请求暂未考虑缓存请求到的数据
干货来了:
1----->基本的post,get请求--->.h
/**
* 网络请求的实例方法
*
* @param type get / post
* @param urlString 请求的地址
* @param paraments 请求的参数
* @param successBlock 请求成功的回调
* @param failureBlock 请求失败的回调
* @param progress 进度
*/
+(
void
)requestWithType:(HttpRequestType)type withUrlString:(
NSString
*)urlString withParaments:(
id
)paraments withSuccessBlock:( requestSuccess)successBlock withFailureBlock:( requestFailure)failureBlock progress:(downloadProgress)progress;
/**
* 网络请求的实例方法
*
* @param type get / post
* @param urlString 请求的地址
* @param paraments 请求的参数
* @param successBlock 请求成功的回调
* @param failureBlock 请求失败的回调
* @param progress 进度
*/
+(
void
)requestWithType:(HttpRequestType)type withUrlString:(
NSString
*)urlString withParaments:(
id
)paraments withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock progress:(downloadProgress)progress
{
switch
(type) {
case
HttpRequestTypeGet:
{
[[NetWorkManager shareManager] GET:urlString parameters:paraments progress:^(
NSProgress
* _Nonnull downloadProgress) {
progress(downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
} success:^(
NSURLSessionDataTask
* _Nonnull task,
id
_Nullable responseObject) {
successBlock(responseObject);
} failure:^(
NSURLSessionDataTask
* _Nullable task,
NSError
* _Nonnull error) {
failureBlock(error);
}];
break
;
}
case
HttpRequestTypePost:
{
[[NetWorkManager shareManager] POST:urlString parameters:paraments progress:^(
NSProgress
* _Nonnull uploadProgress) {
progress(uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
} success:^(
NSURLSessionDataTask
* _Nonnull task,
id
_Nullable responseObject) {
successBlock(responseObject);
} failure:^(
NSURLSessionDataTask
* _Nullable task,
NSError
* _Nonnull error) {
failureBlock(error);
}];
}
}
}
2----->上传图片
.h
/**
* 上传图片
*
* @param operations 上传图片预留参数---视具体情况而定 可移除
* @param imageArray 上传的图片数组
* @parm width 图片要被压缩到的宽度
* @param urlString 上传的url
* @param successBlock 上传成功的回调
* @param failureBlock 上传失败的回调
* @param progress 上传进度
*/
+(
void
)uploadImageWithOperations:(
NSDictionary
*)operations withImageArray:(
NSArray
*)imageArray withtargetWidth:(CGFloat )width withUrlString:(
NSString
*)urlString withSuccessBlock:(requestSuccess)successBlock withFailurBlock:(requestFailure)failureBlock withUpLoadProgress:(uploadProgress)progress;
/**
* 上传图片
*
* @param operations 上传图片等预留参数---视具体情况而定 可移除
* @param imageArray 上传的图片数组
* @parm width 图片要被压缩到的宽度
* @param urlString 上传的url---请填写完整的url
* @param successBlock 上传成功的回调
* @param failureBlock 上传失败的回调
* @param progress 上传进度
*
*/
+(
void
)uploadImageWithOperations:(
NSDictionary
*)operations withImageArray:(
NSArray
*)imageArray withtargetWidth:(CGFloat )width withUrlString:(
NSString
*)urlString withSuccessBlock:(requestSuccess)successBlock withFailurBlock:(requestFailure)failureBlock withUpLoadProgress:(uploadProgress)progress;
{
//1.创建管理者对象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:urlString parameters:operations constructingBodyWithBlock:^(
id
<AFMultipartFormData> _Nonnull formData) {
NSUInteger
i = 0 ;
/**出于性能考虑,将上传图片进行压缩*/
for
(UIImage * image in imageArray) {
//image的分类方法
UIImage * resizedImage = [UIImage IMGCompressed:image targetWidth:width];
NSData
* imgData = UIImageJPEGRepresentation(resizedImage, .5);
//拼接data
[formData appendPartWithFileData:imgData name:[
NSString
stringWithFormat:
@"picflie%ld"
,(
long
)i] fileName:
@"image.png"
mimeType:
@" image/jpeg"
];
i++;
}
} progress:^(
NSProgress
* _Nonnull uploadProgress) {
progress(uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
} success:^(
NSURLSessionDataTask
* _Nonnull task,
NSDictionary
* _Nullable responseObject) {
successBlock(responseObject);
} failure:^(
NSURLSessionDataTask
* _Nullable task,
NSError
* _Nonnull error) {
failureBlock(error);
}];
}
3---->视频上传实现
.h
/**
* 视频上传
*
* @param operations 上传视频预留参数---视具体情况而定 可移除
* @param videoPath 上传视频的本地沙河路径
* @param urlString 上传的url
* @param successBlock 成功的回调
* @param failureBlock 失败的回调
* @param progress 上传的进度
*/
+(
void
)uploadVideoWithOperaitons:(
NSDictionary
*)operations withVideoPath:(
NSString
*)videoPath withUrlString:(
NSString
*)urlString withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock withUploadProgress:(uploadProgress)progress;
.m
/**
* 视频上传
*
* @param operations 上传视频预留参数---视具体情况而定 可移除
* @param videoPath 上传视频的本地沙河路径
* @param urlString 上传的url
* @param successBlock 成功的回调
* @param failureBlock 失败的回调
* @param progress 上传的进度
*/
+(
void
)uploadVideoWithOperaitons:(
NSDictionary
*)operations withVideoPath:(
NSString
*)videoPath withUrlString:(
NSString
*)urlString withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock withUploadProgress:(uploadProgress)progress
{
/**获得视频资源*/
AVURLAsset * avAsset = [AVURLAsset assetWithURL:[
NSURL
URLWithString:videoPath]];
/**压缩*/
// NSString *const AVAssetExportPreset640x480;
// NSString *const AVAssetExportPreset960x540;
// NSString *const AVAssetExportPreset1280x720;
// NSString *const AVAssetExportPreset1920x1080;
// NSString *const AVAssetExportPreset3840x2160;
AVAssetExportSession * avAssetExport = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPreset640x480];
/**创建日期格式化器*/
NSDateFormatter
* formatter = [[
NSDateFormatter
alloc] init];
[formatter setDateFormat:
@"yyyy-MM-dd-HH:mm:ss"
];
/**转化后直接写入Library---caches*/
NSString
* videoWritePath = [[
NSSearchPathForDirectoriesInDomains
(
NSCachesDirectory
,
NSUserDomainMask
,
YES
) firstObject] stringByAppendingString:[
NSString
stringWithFormat:
@"/output-%@.mp4"
,[formatter stringFromDate:[
NSDate
date]]]];
avAssetExport.outputURL = [
NSURL
URLWithString:videoWritePath];
avAssetExport.outputFileType = AVFileTypeMPEG4;
[avAssetExport exportAsynchronouslyWithCompletionHandler:^{
switch
([avAssetExport status]) {
case
AVAssetExportSessionStatusCompleted:
{
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
[manager POST:urlString parameters:operations constructingBodyWithBlock:^(
id
<AFMultipartFormData> _Nonnull formData) {
//获得沙盒中的视频内容
[formData appendPartWithFileURL:[
NSURL
fileURLWithPath:videoWritePath] name:
@"write you want to writre"
fileName:videoWritePath mimeType:
@"video/mpeg4"
error:
nil
];
} progress:^(
NSProgress
* _Nonnull uploadProgress) {
progress(uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
} success:^(
NSURLSessionDataTask
* _Nonnull task,
NSDictionary
* _Nullable responseObject) {
successBlock(responseObject);
} failure:^(
NSURLSessionDataTask
* _Nullable task,
NSError
* _Nonnull error) {
failureBlock(error);
}];
break
;
}
default
:
break
;
}
}];
}
4----->文件下载
.h
/**
* 文件下载
*
* @param operations 文件下载预留参数---视具体情况而定 可移除
* @param savePath 下载文件保存路径
* @param urlString 请求的url
* @param successBlock 下载文件成功的回调
* @param failureBlock 下载文件失败的回调
* @param progress 下载文件的进度显示
*/
+(
void
)downLoadFileWithOperations:(
NSDictionary
*)operations withSavaPath:(
NSString
*)savePath withUrlString:(
NSString
*)urlString withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock withDownLoadProgress:(downloadProgress)progress
.m
/**
* 文件下载
*
* @param operations 文件下载预留参数---视具体情况而定 可移除
* @param savePath 下载文件保存路径
* @param urlString 请求的url
* @param successBlock 下载文件成功的回调
* @param failureBlock 下载文件失败的回调
* @param progress 下载文件的进度显示
*/
+(
void
)downLoadFileWithOperations:(
NSDictionary
*)operations withSavaPath:(
NSString
*)savePath withUrlString:(
NSString
*)urlString withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock withDownLoadProgress:(downloadProgress)progress
{
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
[manager downloadTaskWithRequest:[
NSURLRequest
requestWithURL:[
NSURL
URLWithString:urlString]] progress:^(
NSProgress
* _Nonnull downloadProgress) {
progress(downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
} destination:^
NSURL
* _Nonnull(
NSURL
* _Nonnull targetPath,
NSURLResponse
* _Nonnull response) {
return
[
NSURL
URLWithString:savePath];
} completionHandler:^(
NSURLResponse
* _Nonnull response,
NSURL
* _Nullable filePath,
NSError
* _Nullable error) {
if
(error) {
failureBlock(error);
}
}];
}
5---->取消指定的url请求
.h
/**
* 取消指定的url请求
*
* @param requestType 该请求的请求类型
* @param string 该请求的url
*/
+(
void
)cancelHttpRequestWithRequestType:(
NSString
*)requestType requestUrlString:(
NSString
*)string;
/**
* 取消指定的url请求
*
* @param requestType 该请求的请求类型
* @param string 该请求的完整url
*/
+(
void
)cancelHttpRequestWithRequestType:(
NSString
*)requestType requestUrlString:(
NSString
*)string
{
NSError
* error;
/**根据请求的类型 以及 请求的url创建一个NSMutableURLRequest---通过该url去匹配请求队列中是否有该url,如果有的话 那么就取消该请求*/
NSString
* urlToPeCanced = [[[[NetWorkManager shareManager].requestSerializer requestWithMethod:requestType URLString:string parameters:
nil
error:&error] URL] path];
for
(
NSOperation
* operation in [NetWorkManager shareManager].operationQueue.operations) {
//如果是请求队列
if
([operation isKindOfClass:[
NSURLSessionTask
class
]]) {
//请求的类型匹配
BOOL
hasMatchRequestType = [requestType isEqualToString:[[(
NSURLSessionTask
*)operation currentRequest] HTTPMethod]];
//请求的url匹配
BOOL
hasMatchRequestUrlString = [urlToPeCanced isEqualToString:[[[(
NSURLSessionTask
*)operation currentRequest] URL] path]];
//两项都匹配的话 取消该请求
if
(hasMatchRequestType&&hasMatchRequestUrlString) {
[operation cancel];
}
}
}
}
参考网址:http://code4app.com/thread-8424-1-1.html
0 0
- AFN3.0API调用
- afn3.0
- AFN3.0封装
- AFN3.0续点下载
- iOS网络请求(AFN3.0版本)
- AFN3.0以上版本的二次封装
- AFN3.0设置请求头Header
- Swift3.0 + AFN3.0 + MJExtention + (泛型)封装网络请求类
- AFN3.0与2.0分别做了哪些处理
- AFN3.0中文件的下载和上传
- dispatch_group实践,AFN3.0多个网络请求
- AFN3.0 PATCH请求报错Error Domain=NSURLErrorDomain Code=-1012 "(null)"
- iOS开发AFN3.0中json解析返回3840获取error自己解析
- API调用
- piwik api: 调用api数据
- [API]新浪股票api调用
- 调用Windows API
- 调用WIN32 API
- eclipse下struts简单示例
- strcpy,strncpy和strncpy_s的区别
- 一道简单的算法练习
- 感知机讲解
- Windows 没有激活会造成 WSUS 控制台打不开
- AFN3.0API调用
- python GIL 全局锁
- 关于线程的问题
- 使用Windbg寻找高CPU占用线程
- 简单计算器
- 解决Eclipse+JDK 32 bit+hadoop 32bit 遇到org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lan
- [Leetcode] 319. Bulb Switcher
- Ignoring query to other database
- IQKeyboardManager