Example #1
0
        private async void getListTask()
        {
            string url = serverEndPoin + "/tasks";

            // Khởi tạo http client
            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Authorization", userID);
            // Get data
            var response = await httpClient.GetAsync(url);

            var responseContent = await response.Content.ReadAsStringAsync();

            var status = response.StatusCode;

            // Chuyển sang object
            JavaScriptSerializer j = new JavaScriptSerializer();
            var           obj      = j.Deserialize <dynamic>(responseContent);
            List <TaskDT> listTask = new List <TaskDT>();

            foreach (var item in obj["data"])
            {
                TaskDT temp = new TaskDT();
                temp.Id      = item["id"];
                temp.Name    = item["name"];
                temp.DueTime = item["dueTime"];
                temp.Status  = item["status"];
                temp.Tag     = item["tagsList"][0]["name"];
                listTask.Add(temp);
            }
            dgv_list_task.DataSource = listTask;
            switch (status)
            {
            case System.Net.HttpStatusCode.OK:
                break;

            case System.Net.HttpStatusCode.BadRequest:
                string Res = "";
                if (obj["reasons"] != null)
                {
                    foreach (var item in obj["reasons"])
                    {
                        Res = Res + "\n" + item["path"] + " : " + item["message"];
                    }
                }
                MessageBox.Show(obj["message"] + "\n" + Res);
                break;

            case System.Net.HttpStatusCode.RequestTimeout:
                MessageBox.Show("RequestTimeout");
                break;

            case System.Net.HttpStatusCode.InternalServerError:
                MessageBox.Show("InternalServerError");
                break;
            }
        }
Example #2
0
        // GET: Tasks/Delete/5
        public async Task <ActionResult> DeleteAsync()
        {
            ViewBag.Login    = (userID == "") ? false : true;
            this.userID      = Convert.ToString(TempData["UserID"]);
            ViewBag.UserName = TempData["UserName"];
            TempData.Keep();
            ViewBag.UserID = TempData["UserID"];
            TempData.Keep();
            ViewBag.Login = (userID == "") ? false : true;
            string url = serverEndPoin + "/tasks";

            // Khởi tạo http client
            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Authorization", userID);
            // Get data
            var response = await httpClient.GetAsync(url);

            var responseContent = await response.Content.ReadAsStringAsync();

            var status = response.StatusCode;

            // Chuyển sang object
            JavaScriptSerializer j = new JavaScriptSerializer();
            var           obj      = j.Deserialize <dynamic>(responseContent);
            List <TaskDT> listTask = new List <TaskDT>();

            foreach (var item in obj["data"])
            {
                TaskDT temp = new TaskDT();
                temp.Id      = item["id"];
                temp.Name    = item["name"];
                temp.DueTime = item["dueTime"];
                temp.Status  = item["status"];
                temp.Tag     = item["tagsList"][0]["name"];
                listTask.Add(temp);
            }
            ViewBag.DataSource = listTask;
            switch (status)
            {
            case System.Net.HttpStatusCode.OK:
                ViewBag.Status = "Get list Successfull";
                ViewBag.UserId = userID;
                break;

            case System.Net.HttpStatusCode.BadRequest:
                ViewBag.Status = "Get list UNSuccessfull";
                string Res = "";
                if (obj["reasons"] != null)
                {
                    foreach (var item in obj["reasons"])
                    {
                        Res = Res + "\n" + item["path"] + " : " + item["message"];
                    }
                }
                ViewBag.Message = obj["message"] + "\n" + Res;
                break;

            case System.Net.HttpStatusCode.RequestTimeout:
                ViewBag.Status  = "Get list UNSuccessfull";
                ViewBag.Message = "RequestTimeout";
                break;

            case System.Net.HttpStatusCode.InternalServerError:
                ViewBag.Status  = "Get list UNSuccessfull";
                ViewBag.Message = "InternalServerError";
                break;

            default:
                ViewBag.Message = obj["message"];
                break;
            }
            return(View());
        }