gpt4 book ai didi

iphone - 如何在iphone上缓存xml数据

转载 作者:行者123 更新时间:2023-11-28 18:43:37 25 4
gpt4 key购买 nike

我正在尝试使用 ASIHTTPRequest 库中的 ASIDownloadCache。我想我几乎已经设置好了,但是我打印到日志中的数据是一堆数字。我认为这可能是格式问题。但我想先让有更多经验的人来运行它以确保我的做法是正确的,希望能帮助我解决问题。

下面的代码向您展示了我是如何设置缓存的,我将这个 View 用于多个数据集,因此需要使用 if 语句,以便我只对特定数据设置缓存。

- (IBAction)setRequestString:(NSString *)string
{
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://***.***.***.***:8888/codeData/"]; // iphone development

//PHP file name is being set from the parent view
[databaseURL appendString:string];

//call ASIHTTP delegates (Used to connect to database)
NSURL *url = [NSURL URLWithString:databaseURL];

checkDataSet = [[NSString alloc] initWithFormat:string]; //Loads ICMfg.xml into checkDataSet for setting up cache

//Create If statments here
if ([checkDataSet isEqualToString:@"ICMfg.xml"]) {

//Cache stuff goes in here
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days

[request setDelegate:self]; // A delegate must be specified
[request startSynchronous];
//[request setDidFinishSelector:@selector(requestFinished:)]; // And an appropriate

}
else
{
//this else statments lets all of the other datasets come through here
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request setDelegate:self];
[request startAsynchronous];
}

}

从这里开始,当 [checkDataSet isEqualToString:@"ICMfg.xml"] 为真时,它将设置缓存参数,然后调用以下方法,我准备好解析我的信息

- (void)requestFinished:(ASIHTTPRequest *)request
{
if ([checkDataSet isEqualToString:@"ICMfg.xml"]) {
BOOL success = [request didUseCachedResponse];
NSLog(@"------------>>>>>>> Success is %@\n", (success ? @"YES" : @"NO"));


responseString = [request responseString];
capturedResponseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%@", capturedResponseData); //this prints out the weird data.

[self startTheParsingProcess:capturedResponseData];

}
else
{
responseString = [request responseString]; //Pass requested text from server over to NSString
capturedResponseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];

[self startTheParsingProcess:capturedResponseData];
}
}

从这里,我检查我的 nslog 以查看该 NSlog 的结果,它吐出一堆数字,下面是输出的一小部分。对我来说,下一步是检查是否有任何内容正在被解析……还要看看缓存是否在工作……然后我需要在您的帮助下弄清楚如何正确格式化数据,如果那是我的主要问题..

另外我想问一下如何让它异步工作,因为目前我只能让它同步工作。

2011-11-09 09:29:55.216 code[3968:207] ------------>>>>>>> Success is YES
2011-11-09 09:29:55.239 code[3968:207] <3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0d0a 3c494345 6e673e3c 52657375 6c742044 42566572 73696f6e 3d223132 33223e3c 5461626c 65733e3c 5461626c 65205461 626c654e 616d653d 2249434d 6667223e 3c526f77 733e3c52 6f77204d 414e5546 41435455 52455249 443d2237 30362220 4d414e55 46414354 55524552 3d22412d 445a4722 2049534c 4f434b4d 414e5546 41435455 5245523d 22462220 49535645 4849434c 453d2246 223e3c2f 526f773e 3c526f77 204d414e 55464143 54555245 5249443d 22333138 22204d41 4e554641 43545552 45523d22 412e522e 452e2220 49534c4f 434b4d41 4e554641 43545552 45523d22 46222049

如有任何帮助,我们将不胜感激。

最佳答案

我没有看到任何立即在您的代码中显示为错误的内容。

NSLog() 正在打印一个 NSData 对象,它是二进制数据,所以您看到的十六进制数是字节的表示形式,这正是您想要的期待。

NSData Class Reference :

description

Returns an NSString object that contains a hexadecimal representation of the receiver’s contents.

  • (NSString *)description

Return Value

An NSString object that contains a hexadecimal representation of the receiver’s contents in NSData property list format.

如果你想打印出这个数据的字符串表示,使用:

NSString *capturedResponseString = [NSString stringWithUTF8String:[capturedResponseData bytes]];

关于iphone - 如何在iphone上缓存xml数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8056937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com