public async Task <IHttpActionResult> GetBusDynamic(string cityEN, string routeName) { //Initial IHttpActionResult responseResult; DateTime now = DateTime.Now; IBusDynamic repos = DataFactory.BusDynamicRepository(); //Setting target Url string targetURI = ConfigurationManager.AppSettings["BusDynamicInfoURL"].ToString() + "/" + cityEN + "/" + routeName + "?$format=JSON"; HttpClient client = new HttpClient(); client.MaxResponseContentBufferSize = Int32.MaxValue; //Get Json String var response = await client.GetStringAsync(targetURI); //Deserialize var collection = JsonConvert.DeserializeObject <IEnumerable <BusDynamicDeserialize> >(response); //將需要的欄位取出後序列化 var jsonSerialize = JsonConvert.SerializeObject(repos.GetBusDynamicInfo(collection)); //做成JSON字串包裝到最後輸出 StringContent responseMsgString = new StringContent(jsonSerialize, System.Text.Encoding.UTF8, "application/json"); HttpResponseMessage responseMsg = new HttpResponseMessage() { Content = responseMsgString }; responseResult = ResponseMessage(responseMsg); return(responseResult); }
/// <summary> /// 動態公車項目 /// </summary> /// <returns></returns> // GET: BusDynamic public async Task <ActionResult> Index() { var BusDynamicSource = await GetBusDynamicData(); //將JSON反序列化的資料填進資料庫中 using (IBusDynamic repos = DataFactory.BusDynamicRepository()) { repos.AddBusInfo(BusDynamicSource); } return(View()); }
/// <summary> /// 取得路線公車動態資訊 /// </summary> /// <param name="routeName"></param> /// <returns></returns> public async Task <ActionResult> GetDynamicBusInfo(string cityEN, string routeName) { //initial variable DateTime now = DateTime.Now; IBusDynamic repos = DataFactory.BusDynamicRepository(); //Setting target Url string targetURI = ConfigurationManager.AppSettings["BusDynamicInfoURL"].ToString() + "/" + cityEN + "/" + routeName + "?$format=JSON"; HttpClient client = new HttpClient(); client.MaxResponseContentBufferSize = Int32.MaxValue; //Get Json String var response = await client.GetStringAsync(targetURI); //Deserialize var collection = JsonConvert.DeserializeObject <IEnumerable <BusDynamicDeserialize> >(response); return(Content(JsonConvert.SerializeObject(repos.GetBusDynamicInfo(collection)), "application/json")); }