Iphone dev:如何从谷歌货币转换器获得结果?

发布于 2024-10-31 21:26:30 字数 829 浏览 5 评论 0原文

对不起我的英语^^ 我想知道如何从 Google 货币转换器工具中获取结果,该工具位于 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"];
}

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

Sorry for my english ^^
I would know how I can have a result from the google converter tool for currency, available on http://www.google.com/finance/converter?a=1&from=USD&to=GBP for example.

I have already tried something like that :

    -(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"];
}

But when I click on my button, the label does not have a text (it is like it is empty).
Am I doing something wrong ?
Is it possible what I want to do ?
Thank you very much for your futures answers !
Olivier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

灯下孤影 2024-11-07 21:26:30

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

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

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

这个 URL 将返回 JSON

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

解析它即可。 :)

Yeah.. It is possible...
See the data that you are getting is an HTML file or can be treated as xml. Just parse that xml file and get the result. There ia lot of things in that file but you have to only extract

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

Hey try this URL
http://www.google.com/ig/calculator?hl=en&q=1USD=?INR

This URL will return the JSON

{lhs: "1 U.S. dollar",rhs: "44.5097254 Indian rupees",error: "",icc: true}

Just parse it. :)

烙印 2024-11-07 21:26:30

尝试...

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

然后检查您是否收到了响应

NSLog(@"%@", response);

更好的是,删除 NSData alloc/init

替换为

 

NSData *data=[NSData dataWithContentsOfURL:myURL];

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

Try...

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

Then check you've got something in response

NSLog(@"%@", response);

Better yet, remove the NSData alloc/init

Replace with

NSData *data=[NSData dataWithContentsOfURL:myURL];

And, you'll need to parse the return result, because you're not going to get back what you expect. Response will contain HTML page.

泛滥成性 2024-11-07 21:26:30

您最好使用异步请求以避免阻塞您的应用程序。轻松使用 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"];

}

You better use an asynchronous request to avoid blocking your app. Easy using 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"];

}

寒尘 2024-11-07 21:26:30
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 响应。

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);
}

Try this.
This API is giving HTML responce.

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