将股票报价下载到App [英] Downloading Stock quotes to App

查看:140
本文介绍了将股票报价下载到App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS编程的新手,想在我正在设计的iPad应用中显示股票报价和股票详细信息。我正在寻找两种选择我正在看谷歌金融或雅虎财经。

I'm new to iOS programming and want to display stock quotes and stock details in the iPad app I'm designing. I am looking at two options Im looking at google finance or yahoo finance.

我如何向他们的网站发送网址请求然后在Xcode中处理回复?

How would I go about sending a url request to their sites and then handling the reply in Xcode?

我看到雅虎有一个YQL服务,但我无法理解我是如何让它工作尽管在线阅读一些雅虎文档所以我希望避免它。我看过谷歌api,但我无法弄清楚如何整合它并让它来请求股票详细信息。我已经决定通过url请求路由并尝试编写一些基本代码来处理来自url请求的回复,因为这似乎是最简单的选项(虽然我不知道这在搜索股票时会如何工作) 。

I see that yahoo have a YQL service but I can't wrap my head around how I would get it to work despite reading some yahoo documentation online so Im looking to avoid it. Ive had a look at the google api but I can't figure out how to integrate it and get it to request stock details. I've decided to go through the url request route and try to make up some basic code to handle the reply from a url request as this seems like the simplest option (though I don't know how this will work when searching for stocks).

推荐答案

刚刚删除了我的整个答案并重写了以避免混淆:

Just deleted my whole answer and rewrote it to avoid confusion:

我研究了如何使用YQL来查询yahoo finance API,这就是我最终的结果:

I looked into how to use YQL to query the yahoo finance API and here's what I ended up with:

这是完整制定请求字符串的完整代码。你可以将它直接扔到NSURL中以获取NSMutableURLRequest,并获得json响应。此代码将获取每个股票代码的每个属性。要更改此设置,您需要在前缀中的此位中指定单个属性而不是*(选择%20 *%20)。
我从这篇文章。我修改了代码以适应异步请求(也稍微改变了一下,因为它的一部分似乎过时而且无法正常工作。

Here is the completed code to fully formulate the request string. You can throw this directly into the NSURL for a NSMutableURLRequest and will get a json response. This code will fetch every property of each ticker. To change this you will need to specify individual properties instead of the * in this bit in the prefix (select%20*%20). I took part of it from the sample code in this post. I modified the code to fit into an asynchronous request (also changed it slightly because part of it seemed outdated and wasn't working.

#define QUOTE_QUERY_PREFIX @"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20("
#define QUOTE_QUERY_SUFFIX @")%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json"

+ (NSString *)formulateYQLRequestFor:(NSArray *)tickers
{
    NSMutableString *query = [[NSMutableString alloc] init];
    [query appendString:QUOTE_QUERY_PREFIX];
    for (int i = 0; i < [tickers count]; i++) 
    {
        NSString *ticker = [tickers objectAtIndex:i];
        [query appendFormat:@"%%22%@%%22", ticker];
        if (i != [tickers count] - 1)
        {
            [query appendString:@"%2C"];
        }
    }
    [query appendString:QUOTE_QUERY_SUFFIX];

    return query;
}

您可以通过以下方式调用此方法:

You would invoke this by doing something like:

NSArray *tickerArray = [[NSArray alloc] initWithObjects:@"AAPL", @"VZ", nil];
NSString *queryURL = [MyClass formulateYQLRequestFor:tickerArray];

使用这个答案,了解如何制定请求并使用返回的json。
基本上你需要改变的部分是

Use this answer to see how to formulate the request and consume the json that comes back. Essentially the part you need to change is

NSURL *url = [NSURL URLWithString:queryURL];

您还没有发送JSON,因此您应该更改请求以反映该情况。

You're also not sending JSON over so you should to change the request to reflect that.

这篇关于将股票报价下载到App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆