iOS 从网页下载数据的问题,NSURL

发布于 2024-12-07 03:12:08 字数 2957 浏览 1 评论 0原文

需要您的快速建议。我正在创建一个“货币转换器”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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-12-14 03:12:08

您不应该与所有请求共享 responseData,因为它们是异步发送的,它们都会在随机时间完成,并且您可能将所有收到的数据(来自所有请求)写入 响应数据。每个请求都应该有自己的资源(resourceDatatheConnection)。

查看 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) in responseData. Each request should have its own resources (resourceData, theConnection).

Have a look at ASIHTTPRequest for a simple solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文