Issue
So I am using c# xamarin and making a basic stock app that can pull stock quotes using the Alpha Vantage API. I have what I thought might work but it isn't working at all. Yes this is a school project so I don't expect people to just do it. The purpose of this app is to use the API and I need to show the data that it provides after the user enters in a stock symbol from the first page of the app. I need to send that symbol with the api and I am not sure I am pulling in JSON object and I don't know how to get to each field in the object when I do retrieve the JSON correctly.This code is what im trying and I am not getting any information populated into any of my textViews.
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; }
}
}
Solution
The JSON that's being returned from that endpoint doesn't quite match your model.
Here's the line where you tell your program how to parse the response:
Stock currentStockInfo = JsonConvert.DeserializeObject(reader.ReadToEnd());
...but the response looks like this:
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2018-12-10 16:00:02",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2018-12-10": {
"1. open": "104.8000",
"2. high": "107.9800",
"3. low": "103.8900",
"4. close": "107.5900",
"5. volume": "39050766"
},
"2018-12-07": {
"1. open": "108.3800",
"2. high": "109.4500",
"3. low": "104.3000",
"4. close": "104.8200",
"5. volume": "45044937"
}...
...
...
...
}
You're trying to turn this entire response into one Stock
object, which isn't going to work. The response isn't a Stock
, it's a response with 2 objects, one of which has a lot of Stock
objects.
You could try to create a model for this*, but I'd recommend turning this entire response into a JObject
(another object in NewtonSoft JSON.Net).
Here's a DotNetFiddle I put together to demonstrate how it works.
https://dotnetfiddle.net/Iz8UsD
Let me know if I can add anything more helpful.
EDIT: * You can probably get a strongly typed model to work here,the trouble is that each Stock
has a different JSON name. I don't know how to get the parser to parse each as a Stock
while still keeping the data around. I'll have to play with that tonight.
Answered By - Joe Cullinan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.