AFNetworking超时时间设置

来源:互联网 发布:路由器测试软件 编辑:程序博客网 时间:2024/05/17 22:10

首先要了解下AFNetworking的整体结构:

CORE:

AFURLConnectionOperation:一个 NSOperation 实现了NSURLConnection 的代理方法.

HTTP Requests:

AFHTTPRequestOperation:AFURLConnectionOperation的子类,request使用的协议为HTTPHTTPS,它压缩了用于决定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",[urlRequesttimeoutInterval]);  默认就是60s。

.....

}

如何修改超时时间呢?

json方式为例:

 //创建请求并设置超时时间

NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];  

AFJSONRequestOperation *jsonOper = [AFJSONRequestOperation JSONRequestOperationWithRequest:requestsuccess:^(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,longlong))progressBlock completed:(void (^)(UIImage *,NSData *,NSError *, BOOL))completedBlock函数。

主要是:

- (id)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;中的timeoutInterval决定。


0 3