Demo 百度云地址:
http://pan.baidu.com/s/1hqghijq
Demo 微云地址: http://url.cn/KVOwQy
请帮忙看看,谢谢!
我调用的是别人封装的NetWebServiceRequest,在NetWebServiceRequest.m里面调用ASIHTTPRequest ,缓存已经都设置好了。
代码如下:
1.NetWebServiceRequest.m:
2.AppDelegate.m
3.ASIDownloadCache.m
Demo 微云地址: http://url.cn/KVOwQy
请帮忙看看,谢谢!
我调用的是别人封装的NetWebServiceRequest,在NetWebServiceRequest.m里面调用ASIHTTPRequest ,缓存已经都设置好了。
代码如下:
1.NetWebServiceRequest.m:
_runningRequest = [ASIHTTPRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[_runningRequest setCacheStoragePolicy: ASICachePermanentlyCacheStoragePolicy];
[_runningRequest setSecondsToCache:60 * 10];
[_runningRequest setDownloadCache: appDelegate.myCache];
[_runningRequest setDelegate: self];
//SOAP
[_runningRequest addRequestHeader:@"Host" value:[url host]];
[_runningRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
[_runningRequest addRequestHeader:@"Content-Length" value:msgLength];
[_runningRequest addRequestHeader:@"SOAPAction" value:[NSString stringWithFormat:@"%@",soapActionURL]];
[_runningRequest setRequestMethod:@"POST"];
[_runningRequest appendPostData:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
[_runningRequest setValidatesSecureCertificate:NO];
[_runningRequest setTimeOutSeconds:60.0];
[_runningRequest setDefaultResponseEncoding:NSUTF8StringEncoding];
2.AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//set Cache
_myCache = [[ASIDownloadCache alloc] init];
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
[_myCache setStoragePath:[documentDirectory stringByAppendingPathComponent:@"myCache"]];
[_myCache setDefaultCachePolicy: ASIAskServerIfModifiedWhenStaleCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy];
//--
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
3.ASIDownloadCache.m
- (void)setStoragePath:(NSString *)path
{
[[self accessLock] lock];
// [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
[storagePath release];
storagePath = [path retain];
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
BOOL isDirectory = NO;
NSArray *directories = [NSArray arrayWithObjects:path,[path stringByAppendingPathComponent:sessionCacheFolder],[path stringByAppendingPathComponent:permanentCacheFolder],nil];
for (NSString *directory in directories) {
BOOL exists = [fileManager fileExistsAtPath:directory isDirectory:&isDirectory];
if (exists && !isDirectory) {
[[self accessLock] unlock];
[NSException raise:@"FileExistsAtCachePath" format:@"Cannot create a directory for the cache at '%@', because a file already exists",directory];
} else if (!exists) {
[fileManager createDirectoryAtPath:directory withIntermediateDirectories:NO attributes:nil error:nil];
if (![fileManager fileExistsAtPath:directory]) {
[[self accessLock] unlock];
[NSException raise:@"FailedToCreateCacheDirectory" format:@"Failed to create a directory for the cache at '%@'",directory];
}
}
}
// [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
[[self accessLock] unlock];
}