在网上看到的。最近比较忙。 有空的时候整理一下、
首先要了解下AFNetworking的整体结构:
CORE:
AFURLConnectionOperation:一个 NSOperation 实现了NSURLConnection 的代理方法.
HTTP Requests:
AFHTTPRequestOperation:AFURLConnectionOperation的子类,当request使用的协议为HTTP和HTTPS时,它压缩了用于决定request是否成功的状态码和内容类型.
AFJSONRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理jason response数据.
AFXMLRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理xml response数据.
AFPropertyListRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理property list response数据.
HTTP CLIENT:
AFHTTPClient:捕获一个基于http协议的网络应用程序的公共交流模式.
IMAGES
AFImageRequestOperation:一个AFHTTPRequestOperation的子类,用于下载和处理图片.
UIImageView+AFNetworking:添加一些方法到UIImageView中,为了从一个URL中异步加载远程图片。
//
了解以上信息后才来设置超时时间;
1.队列方式的超时时间设置
AFNetworking的默认超时时间为60s。我们可以通过在AFURLConnectionOperation.m文件
- (id)initWithRequest:(NSURLRequest *)urlRequest {
.......
NSLog ( @"timeoutInterval: %f" ,[urlRequest timeoutInterval ]); 默认就是60s。.....
}
如何修改超时时间呢?
json方式为例:
//创建请求并设置超时时间
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
AFJSONRequestOperation *jsonOper = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}]; 2。图片下载方式的超时设置
在SDWebImageDownloader.m文件中的 downloadImageWithURL。
具体为:- (id<SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger,long long))progressBlock completed:(void (^)(UIImage *,NSData *, NSError *, BOOL))completedBlock函数。
主要是:
- (id)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;中的timeoutInterval决定。
iOS开发中如何设置请求超时时间
1 NSString *baseUrl; 2 NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 3 NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; 4 AFJSONRequestOperation *jsonOper = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 5 6 } 8 failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 9 10 }];
以上是AFN的设置请求超时时间.
对于SDWebImage来说
在在SDWebImageDownloader.m文件中的 downloadImageWithURL.
具体为:
1 - (id<SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger,long long))progressBlock completed:(void (^)(UIImage *,NSData *, NSError *, BOOL))completedBlock函数。
主要是:
1 - (id)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;中的timeoutInterval
决定。
如果请求超时之后我们可以取消AFN的请求,通过以下代码实现:
1 // 1.创建AFN管理者 2 AFHTTPRequestOperationManager *mange = [AFHTTPRequestOperationManager manager]; 3 4 // 2.发送请求 5 [mange GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 6 // 请求成功 7 IWLog(@"请求成功"); 8 if (success) { 9 success(responseObject); 10 } 11 12 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 13 14 IWLog(@"请求失败"); 15 if (failure) { 16 failure(error); 17 } 18 }]; 19 20 //3.取消请求 21 [mange.operationQueue cancelAllOperations];