【问题标题】:Iphone dev : how can I get a result from google currency converter?Iphone dev : 如何从谷歌货币转换器获得结果?
【发布时间】:2011-04-12 09:02:25
【问题描述】:

对不起我的英语^^ 我会知道如何从谷歌货币转换器工具中获得结果,例如http://www.google.com/finance/converter?a=1&from=USD&to=GBP

我已经尝试过类似的方法:

    -(IBAction)getResponse:(id) sender{

     NSString *myURLString = @"http://www.google.com/finance/converter?a=1&from=USD&to=GBP";

     NSURL *myURL =  [NSURL URLWithString: myURLString];

     NSData *data = [NSData alloc];

     data=[NSData dataWithContentsOfURL:myURL];


     if(data!=nil && data != ""){

          NSString *response = [[[NSString alloc] initWithData:data]]

          [label setText: response];

     }

     else [label setText:@"not working"];
}

但是当我点击我的按钮时,标签没有文字(就像它是空的)。 难道我做错了什么 ? 我想做的事有可能吗? 非常感谢您对未来的回答! 奥利维尔。

【问题讨论】:

  • 您知道这将返回一个完整的 HTML 页面。请参阅此页面,其中包含使用 google 转换器 API 的示例:oohhyeah.blogspot.com/2009/01/…
  • NSLog 你的回复,看看你得到了什么。检查它是否是 NSString,我认为不是。

标签: iphone currency converter


【解决方案1】:

是的……有可能…… 查看您获取的数据是 HTML 文件还是可以视为 xml。只需解析该 xml 文件并获得结果。该文件中有很多东西,但您只需提取

<div id=currency_converter_result>1 USD = <span class=bld>0.6118 GBP</span> 

嘿试试这个网址 http://www.google.com/ig/calculator?hl=en&q=1USD=?INR

此 URL 将返回 JSON

{lhs: "1 美元",rhs: "44.5097254 印度卢比",error: "",icc: true}

只需解析它。 :)

【讨论】:

    【解决方案2】:

    试试……

    NSData *data = [[NSData alloc] init];

    然后检查你有什么回应

    NSLog(@"%@", response);
    

    更好的是,删除 NSData alloc/init

    替换为

     

    NSData *data=[NSData dataWithContentsOfURL:myURL];
    

    而且,您需要解析返回结果,因为您不会得到您期望的结果。响应将包含 HTML 页面。

    【讨论】:

      【解决方案3】:

      您最好使用异步请求来避免阻塞您的应用。轻松使用ASIHTTPRequest

      - (IBAction)grabURLInBackground:(id)sender { NSURL *url = [NSURL URLWithString:@"http://www.google.com/finance/converter?a=1&from=USD&to=GBP"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous]; } - (void)requestFinished:(ASIHTTPRequest *)request { // Use when fetching text data NSString *responseString = [request responseString]; [label setText: responseString]; } - (void)requestFailed:(ASIHTTPRequest *)request { NSError *error = [request error]; //you can log the error description [label setText:@"not working"]; }

      【讨论】:

        【解决方案4】:
        NSString *myURLString = @"http://www.google.com/finance/converter?a=1&from=USD&to=GBP";
        
        NSURL *myURL =  [NSURL URLWithString: myURLString];
        
        NSData *data = [NSData alloc];
        
        data=[NSData dataWithContentsOfURL:myURL];
        
        
        if(data!=nil){
        
            NSString *response = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
        
            NSLog(@"%@",response);
        }
        

        试试这个。 此 API 正在给出 HTML 响应。

        【讨论】:

          猜你喜欢
          • 2015-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多