我开发了一个applikation,它使用XML Parser加载指定的url。 此URL有时会更改,并且每4秒重新加载一次。(通过NSTIMER)
在模拟器上它可以很好地工作,但在设备上它总是一样的(就像一种缓存)
-(void)parseXMLFileAtURL:(NSString *) URL{
NSLog(@"Parsed XML URL: %@", URL);
currentURL=[URL copy];;
tutorials= [[NSMutableArray alloc] init];
settings= [[NSMutableArray alloc] init];
NSURL *xmlURL=[NSURL URLWithString:URL];
xmlParser =[[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[xmlParser setDelegate:self];
[xmlParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
currentElement= [elementName copy];
if([elementName isEqualToString:@"item"]){
item=[[NSMutableDictionary alloc] init];
currentTitle=[[NSMutableString alloc] init];
currentImage=[[NSMutableString alloc] init];
currentLink=[[NSMutableString alloc] init];
currentDetails=[[NSMutableString alloc] init];
}
if([elementName isEqualToString:@"setting"]){
setting=[[NSMutableDictionary alloc] init];
currentTitle=[[NSMutableString alloc] init];
currentTyp=[[NSMutableString alloc] init];
currentLink=[[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"item"]){
NSLog(@"currenttitle: %@",currentTitle );
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentImage forKey:@"image"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentDetails forKey:@"details"];
[tutorials addObject:[item copy]];
}
if([elementName isEqualToString:@"setting"]){
NSLog(@"Currentsettingtitle: %@",currentTitle );
[setting setObject:currentTitle forKey:@"title"];
[setting setObject:currentTyp forKey:@"typ"];
[setting setObject:currentLink forKey:@"link"];
[settings addObject:[setting copy]];
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if([currentElement isEqualToString:@"title"]){
[currentTitle appendString:string];
}
if([currentElement isEqualToString:@"image"]){
[currentImage appendString:string];
}
if([currentElement isEqualToString:@"link"]){
[currentLink appendString:string];
}
if([currentElement isEqualToString:@"typ"]){
[currentTyp appendString:string];
}
if([currentElement isEqualToString:@"details"]){
[currentDetails appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"Fertig mit Document");
for (int i=0; i<[settings count]; i++) {
NSString *typ =(NSString*)[[settings objectAtIndex:i ] objectForKey:@"typ"];
typ=[typ stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
if([typ isEqualToString:@"Button1"])
button1.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
if([typ isEqualToString:@"Button2"])
button2.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
if([typ isEqualToString:@"Button3"])
button3.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
if([typ isEqualToString:@"Titel"])
navitem.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
}
//[[tutorials ] objectForKey:@"title"]
[myTableView reloadData];
}
有人有想法吗?
答案 0 :(得分:3)
您应该尝试使用数据初始化解析器。
使用NSURLRequest和NSURLConnection获取您的数据:
NSURL *xmlURL=[NSURL URLWithString:URL];
NSURLRequest *request = [NSURLRequest requestWithURL:xmlURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:0.0f];
xmlParser =[[NSXMLParser alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//etc.
答案 1 :(得分:0)
这是一些代码(用xml解析无法避免)
但是,从您的问题看来,xml解析没有问题。
那么,关注与Timer相关的代码呢?也许在实际获取新URL时添加一些日志?只是为了确保你收到新的请求???