iOS 从网页下载数据的问题,NSURL
需要您的快速建议。我正在创建一个“货币转换器”iPhone 应用程序来从谷歌货币网站检索数据。下载 USD 字符串完美运行 ->澳元、美元->加元、美元->港元、美元->匈牙利福林、美元->日元。但是,我不明白为什么在尝试检索 USD->KRW 和 USD->ZMK 时不工作并返回NULL。请参考下面的代码。
-(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"];
}
控制台结果:
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)
请帮忙,非常感谢。
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 the 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该与所有请求共享
responseData
,因为它们是异步发送的,它们都会在随机时间完成,并且您可能将所有收到的数据(来自所有请求)写入响应数据
。每个请求都应该有自己的资源(resourceData
、theConnection
)。查看 ASIHTTPRequest 以获得简单的解决方案。
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) inresponseData
. Each request should have its own resources (resourceData
,theConnection
).Have a look at ASIHTTPRequest for a simple solution.