首页 > 解决方案 > Yahoo Finance API调用返回空白?Xamarin 表单

问题描述

我正在使用 Xamarin.Forms 处理股票监视列表项目。在我使用 IEX Cloud API 之前收集股票价格,但我正在尝试使用 Yahoo Finance API。我使用以下代码但result返回空白。

namespace Stock_WatchList
{
    public partial class MainPage : ContentPage
    {
        public string ApplePrice { get; set; }
        public string NioPrice { get; set; }
        public string PalantirPrice { get; set; }
        public string TeslaPrice { get; set; }
        public string XpengPrice { get; set; }
        public string AmazonPrice { get; set; }
        public string PinduoduoPrice { get; set; }
        public string DisneyPrice { get; set; }
        public string AmdPrice { get; set; }
        public string TwitterPrice { get; set; }

        public MainPage()
        {
            InitializeComponent();


            WebClient yahoo = new WebClient();
            string yahooPrices = yahoo.DownloadString("https://query1.finance.yahoo.com/v7/finance/chart/AAPL?interval=5m ");
            MatchCollection YahooMatches = System.Text.RegularExpressions.Regex.Matches(yahooPrices, @"""([^""]+)"":{""quote"":{""latestPrice"":(\d+(?:.\d+))}}");
            foreach (Match match in YahooMatches)
            {
                switch (match.Groups[1].Value)
                {
                    case "regularMarketPrice":
                        ApplePrice = $"$ {match.Groups[2].Value}";
                        break;
                }
            }

            BindingContext = this;
        }

        private async void ImageButton_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new SearchPage());
        }
    }
} 

谁能帮我解决这个问题?

标签: c#xamarin.forms

解决方案


我使用 Postman 调用了该 yahoo api,并以 json 格式接收数据。我能够轻松地从该程序中查看数据,我同意 Jason 的观点,在这种情况下 JSON 解析器会更好地为您服务。


推荐阅读