所以我正在使用 c# xamarin 并制作一个基本的股票应用程序,可以使用 Alpha Vantage API 提取股票报价。我有我认为可能有用的东西,但它根本不起作用。是的,这是一个学校项目,所以我不希望人们只是这样做。这个应用程序的目的是使用 API,我需要在用户从应用程序的第一页输入股票代码后显示它提供的数据。我需要使用 api 发送该符号,但我不确定我是否正在拉入 JSON 对象,并且当我正确检索 JSON 时,我不知道如何访问对象中的每个字段。这段代码是我正在尝试的我没有将任何信息填充到我的任何文本视图中。
namespace Stock_Quote
{
[Activity(Label = "StockInfoActivity1")]
public class StockInfoActivity1 : Activity
{
private ISharedPreferences prefs = Application.Context.GetSharedPreferences("APP_DATA", FileCreationMode.Private);
TextView txtSymbol, txtOpen, txtClose, txtHigh, txtLow, txtVolume;
string webservice_url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=";
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.activity_stock_info);
txtSymbol = FindViewById<TextView>(Resource.Id.txtSymbol);
txtOpen = FindViewById<TextView>(Resource.Id.txtOpen);
txtClose = FindViewById<TextView>(Resource.Id.txtClose);
txtHigh = FindViewById<TextView>(Resource.Id.txtHigh);
txtLow = FindViewById<TextView>(Resource.Id.txtLow);
txtVolume = FindViewById<TextView>(Resource.Id.txtVolume);
string current = prefs.GetString("current", "no stok symbol found");
//txtSymbol.Text = current;
try
{
webservice_url = webservice_url + current + "&apikey=AVALIDAPIKEY";
Uri url = new Uri(webservice_url);
var webRequest = WebRequest.Create(url);
if (webRequest != null)
{
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
//Get the response
WebResponse wr = webRequest.GetResponseAsync().Result;
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream);
Stock currentStockInfo = JsonConvert.DeserializeObject<Stock>(reader.ReadToEnd());
if (currentStockInfo.RestResponse.result == null)
{
txtSymbol.Text = "No stock found";
}
else
{
txtSymbol.Text = current;
txtOpen.Text = currentStockInfo.RestResponse.stockInfo.Open;
txtClose.Text = currentStockInfo.RestResponse.stockInfo.Close;
txtHigh.Text = currentStockInfo.RestResponse.stockInfo.High;
txtLow.Text = currentStockInfo.RestResponse.stockInfo.Low;
txtVolume.Text = currentStockInfo.RestResponse.stockInfo.Volume;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
public class Result
{
public string Information { get; set; }
public string Symbol { get; set; }
public string Last { get; set; }
public string Size { get; set; }
public string TimeZone { get; set; }
}
public class StockInfo
{
public string Open { get; set; }
public string High { get; set; }
public string Low { get; set; }
public string Close { get; set; }
public string Volume { get; set; }
}
public class RestResponse
{
public Result result { get; set; }
public StockInfo stockInfo { get; set; }
}
public class Stock
{
public RestResponse RestResponse { get; set; }
}
}