iOS下载来自网络问题的数据,NSURL

时间:2011-09-30 10:30:50

标签: iphone ios download nsurl

需要你的快速建议。我正在创建一个“货币转换器”iPhone应用程序来检索谷歌货币网站的数据。下载字符串USD - >是完美的工作。 AUD,USD-> CAD,USD-> HKD,USD-> HUF,USD-> JPY。但是,当我尝试检索USD-> KRW和USD-> ZMK时,我不知道 NOT 工作原因并返回 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)

请帮忙,非常感谢。

1 个答案:

答案 0 :(得分:1)

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

查看ASIHTTPRequest的简单解决方案。