When I try to run the code below I am getting an error that says "Cannot implicitly convert type void to "Program.trainDataResult". I am just trying to return my trainDataResult object form the async method so I can use it with other methods in my app. Any suggestions? I am confused as to why the async method is returning void when I have specified that it should return the trainDataResult object wrapped in a task.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
namespace ctaapijim
{
public class Program
{
[XmlRoot(ElementName = "ctatt", DataType = "string", IsNullable = true)]
public class trainDataResult
{
[XmlElement(ElementName = "tmst")]
public string timeStamp { get; set; }
[XmlElement(ElementName = "errCd")]
public byte errorCode { get; set; }
[XmlElement(ElementName = "errNm")]
public string errorName { get; set; }
[XmlElement(ElementName = "eta")]
public trainData eta { get; set; }
}
[Serializable()]
public class trainData
{
[XmlElement(ElementName = "staId")]
public ushort stationId { get; set; }
[XmlElement(ElementName = "stpId")]
public ushort stopId { get; set; }
[XmlElement(ElementName = "staNm")]
public string stationName { get; set; }
[XmlElement(ElementName = "stpDe")]
public string stopDesc { get; set; }
[XmlElement(ElementName = "rn")]
public ushort runNum { get; set; }
[XmlElement(ElementName = "rt")]
public string routeName { get; set; }
[XmlElement(ElementName = "destSt")]
public ushort destStation { get; set; }
[XmlElement(ElementName = "destNm")]
public string destName { get; set; }
[XmlElement(ElementName = "trDr")]
public byte trainDir { get; set; }
[XmlElement(ElementName = "prdt")]
public string prdTime { get; set; }
[XmlElement(ElementName = "arrT")]
public string arrTime { get; set; }
[XmlElement(ElementName = "isApp")]
public ushort isApp { get; set; }
[XmlElement(ElementName = "isSch")]
public ushort isSch { get; set; }
[XmlElement(ElementName = "isDly")]
public ushort isDly { get; set; }
[XmlElement(ElementName = "isFlt")]
public ushort isFlt { get; set; }
[XmlElement(ElementName = "flags")]
public string flags { get; set; }
[XmlElement(ElementName = "lat")]
public double lat { get; set; }
[XmlElement(ElementName = "lon")]
public double lon { get; set; }
[XmlElement(ElementName = "heading")]
public ushort heading { get; set; }
}
private const string URL = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=d94463bf32094ac6bb1417cd8f850a23&mapid=41300&max=1";
static void Main(string[] args)
{
trainDataResult td = RunAsync().Wait();
}
static async Task<trainDataResult> RunAsync()
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(URL);
if (response.IsSuccessStatusCode)
{
//get data from CTA API
var xml = await response.Content.ReadAsStringAsync();
var ds = new XmlSerializer(typeof(trainDataResult), new XmlRootAttribute("ctatt"));
using (StringReader sr = new StringReader(xml))
{
using (XmlReader xr = XmlReader.Create(sr))
{
var trainDataResult = (trainDataResult)ds.Deserialize(xr);
return trainDataResult;
//calculate the difference in time between arrival time and current time
/* trainDataResult.eta.arrTime = trainDataResult.eta.arrTime.Substring(9);
TimeSpan trainTime = TimeSpan.Parse(trainDataResult.eta.arrTime);
var timeOfDay = DateTime.Now.TimeOfDay;
TimeSpan arrMins = (trainTime - timeOfDay);
Console.WriteLine("Station Name: {0}\nRoute Name: {1}\nArrival Time: {2}\nRun Number: {3}\nDirection: {4}", trainDataResult.eta.stationName, trainDataResult.eta.routeName, arrMins, trainDataResult.eta.runNum, trainDataResult.eta.stopDesc);
*/
}
}
}
else
{
Console.WriteLine("There was an error!");
}
}
}
}
}