iOS download data from the web issues, NSURL
Need your quick advise. I am create a "Currency Converter" iPhone app to retrieve the data from google currency websites. It is work perfectly to download t开发者_运维百科he string of USD -> AUD, USD->CAD, USD->HKD, USD->HUF, USD->JPY. However, I don't why is NOT working and return NULL when try to retrieve USD->KRW and USD->ZMK. Please refer the code as below.
-(void)loadData:(NSString*)countryName{
self.responseData = [NSMutableData data];
NSString *responseURL = [NSString stringWithFormat: @"http://www.google.com/ig/calculator?q=1USD=?%@", countryName];
NSLog(@"URL:%@", responseURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:responseURL]];
theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString* responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"This is the responseString %@", responseString);
[responseString release];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData:@"AUD"];
[self loadData:@"CAD"];
[self loadData:@"HKD"];
[self loadData:@"HUF"];
[self loadData:@"JPY"];
[self loadData:@"KRW"];
[self loadData:@"ZMK"];
}
Result from Console:
2011-09-30 18:03:50.877 Converter[1691:f503] URL:http://www.google.com/ig/calculator?q=1USD=?AUD
2011-09-30 18:03:50.879 Converter[1691:f503] URL:http://www.google.com/ig/calculator?q=1USD=?CAD
2011-09-30 18:03:50.879 Converter[1691:f503] URL:http://www.google.com/ig/calculator?q=1USD=?HKD
2011-09-30 18:03:50.879 Converter[1691:f503] URL:http://www.google.com/ig/calculator?q=1USD=?HUF
2011-09-30 18:03:50.879 Converter[1691:f503] URL:http://www.google.com/ig/calculator?q=1USD=?JPY
2011-09-30 18:03:50.879 Converter[1691:f503] URL:http://www.google.com/ig/calculator?q=1USD=?KRW
2011-09-30 18:03:50.879 Converter[1691:f503] URL:http://www.google.com/ig/calculator?q=1USD=?ZMK
2011-09-30 18:03:50.952 Converter[1691:f503] This is the responseString {lhs: "1 U.S. dollar",rhs: "1.02228583 Australian dollars",error: "",icc: true}
2011-09-30 18:03:50.962 Converter[1691:f503] This is the responseString {lhs: "1 U.S. dollar",rhs: "7.79149947 Hong Kong dollars",error: "",icc: true}
2011-09-30 18:03:50.966 Converter[1691:f503] This is the responseString {lhs: "1 U.S. dollar",rhs: "215.889465 Hungarian forints",error: "",icc: true}
2011-09-30 18:03:50.982 Converter[1691:f503] This is the responseString {lhs: "1 U.S. dollar",rhs: "1.03910031 Canadian dollars",error: "",icc: true}
2011-09-30 18:03:50.993 Converter[1691:f503] This is the responseString {lhs: "1 U.S. dollar",rhs: "76.5579544 Japanese yen",error: "",icc: true}
2011-09-30 18:03:51.010 Converter[1691:f503] This is the responseString (null)
2011-09-30 18:03:51.047 Converter[1691:f503] This is the responseString (null)
Please help and much appreciated.
You shouldn't share responseData
with all the requests, since they're sent asynchronously they will all finish at a random time, and you're probably writing all received data (from all requests) in responseData
. Each request should have its own resources (resourceData
, theConnection
).
Have a look at ASIHTTPRequest for a simple solution.
精彩评论