0
我正在使用ASIHTTPRequest從Internet下載數據。如果我失去了互聯網連接,並且我嘗試加載應用程序崩潰的數據。ASIHTTPRequest在中斷Internet連接後導致崩潰
代碼:
- (IBAction)load:(id)sender {
if ([act isAnimating]) {
NSLog(@"Request already being processed!");
}
else {
ASINetworkQueue *queue = [ASINetworkQueue queue];
ASIHTTPRequest *usdRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADUSD=X&f=l1"]];
ASIHTTPRequest *eurRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=l1"]];
ASIHTTPRequest *dateRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=d1"]];
ASIHTTPRequest *timeRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=t1"]];
[queue addOperation:usdRequest];
[queue addOperation:eurRequest];
[queue addOperation:dateRequest];
[queue addOperation:timeRequest];
[queue setQueueDidFinishSelector:@selector(parseLoadedData:)];
[queue setRequestDidFinishSelector:@selector(requestLoaded:)];
[queue setDelegate:self];
[queue go];
[act startAnimating];
}
}
- (void)requestLoaded:(ASIHTTPRequest *)request {
if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADUSD=X&f=l1"]]) {
usdString = [[request responseString] retain];
} else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=l1"]]) {
eurString = [[request responseString] retain];
} else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=d1"]]) {
dateString = [[request responseString] retain];
} else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=t1"]]) {
timeString = [[request responseString] retain];
}
}
- (void)parseLoadedData:(ASIHTTPRequest *)request {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *Date = [dateString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSString *Time = [timeString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSString *total = [NSString stringWithFormat:@"%@ %@",Date,Time];
if ([eurString length] == 0) {
test.text = [defaults objectForKey:@"date"];
eur.text = [defaults objectForKey:@"eur"];
usd.text = [defaults objectForKey:@"usd"];
} else {
test.text = total;
eur.text = eurString;
usd.text = usdString;
[defaults setObject:test.text forKey:@"date"];
[defaults setObject:usd.text forKey:@"usd"];
[defaults setObject:eur.text forKey:@"eur"];
}
[defaults synchronize];
[eurString release];
[usdString release];
[dateString release];
[timeString release];
[act stopAnimating];
}