示例#1
0
        /// <summary>
        /// 取得公車路線資訊並轉成JSON輸出
        /// </summary>
        /// <param name="city">鄉鎮英文</param>
        /// <param name="routeName">公車路線名稱</param>
        /// <param name="direction">去、返(0、1)</param>
        /// <returns></returns>
        public async Task <ActionResult> GetBusEstimatedTime(string city, string routeName = "", int direction = 0)
        {
            //initial variable
            DateTime  now   = DateTime.Now;
            IBusRoute repos = DataFactory.BusRouteRepository();
            int       flag  = 0;

            if (city == "Taipei")
            {
                flag = 1;
            }
            //Setting target Url
            string     targetURI = ConfigurationManager.AppSettings["BusEstimatedTimeURL"].ToString() + "/" + city + "/" + 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 <BusEstimatedTimeDeserialize> >(response);
            string afterNow   = (DateTime.Now - now).ToString();

            return(Content(JsonConvert.SerializeObject(repos.GetBusEstimatedTime(collection, flag)), "application/json"));
        }
示例#2
0
        public async Task <IHttpActionResult> GetBusEstimatedTime(string city, string routeName = "", int direction = 0)
        {
            //initial variable
            IHttpActionResult responseResult;
            DateTime          now   = DateTime.Now;
            IBusRoute         repos = DataFactory.BusRouteRepository();
            int flag = 0;

            if (city == "Taipei")
            {
                flag = 1;
            }
            //Setting target Url
            string     targetURI = ConfigurationManager.AppSettings["BusEstimatedTimeURL"].ToString() + "/" + city + "/" + 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 <BusEstimatedTimeDeserialize> >(response);
            //將需要的欄位取出後序列化
            var jsonSerialize = JsonConvert.SerializeObject(repos.GetBusEstimatedTime(collection, flag));
            //做成JSON字串包裝到最後輸出
            StringContent       responseMsgString = new StringContent(jsonSerialize, System.Text.Encoding.UTF8, "application/json");
            HttpResponseMessage responseMsg       = new HttpResponseMessage()
            {
                Content = responseMsgString
            };

            responseResult = ResponseMessage(responseMsg);

            return(responseResult);
        }