Iphone dev:如何从谷歌货币转换器获得结果?
对不起我的英语^^ 我想知道如何从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是啊..有可能..
查看您获取的数据是 HTML 文件还是可以视为 xml。只需解析该 xml 文件并获取结果即可。该文件中有很多东西,但你必须只提取
嘿尝试这个 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
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. :)
尝试...
然后检查您是否收到了响应
更好的是,删除 NSData alloc/init
替换为
而且,您需要解析返回结果,因为您不会得到您期望的结果。响应将包含 HTML 页面。
Try...
Then check you've got something in response
Better yet, remove the NSData alloc/init
Replace with
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.
您最好使用异步请求以避免阻塞您的应用程序。轻松使用 ASIHTTPRequest:
You better use an asynchronous request to avoid blocking your app. Easy using ASIHTTPRequest:
试试这个。
此 API 提供 HTML 响应。
Try this.
This API is giving HTML responce.