Ejemplo n.º 1
0
        private void GetMessageCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpWebRequest  webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                HttpWebResponse response;

                // End the get response operation
                response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
                response.Headers[HttpRequestHeader.ContentType] = "application/json; charset=UTF-8";

                Stream       streamResponse = response.GetResponseStream();
                StreamReader streamReader   = new StreamReader(streamResponse);
                var          Response       = streamReader.ReadToEnd();

                dynamic json = JsonConvert.DeserializeObject(Response);

                MessageListCollection = new ObservableCollection <Message>();

                foreach (var item in json["list"])
                {
                    var message = new Message();
                    message.ID          = Convert.ToString(item["id"]);
                    message.RichContent = AppGlobalStatic.ImgToEmotion((string)item["richContent"]);

                    message.PrettyTime = AppGlobalStatic.DateStringFromNow(((string)item["prettyTime"]).Replace("今天", DateTime.Now.ToString("yyyy-MM-dd")));

                    message.Type = (string)item["type"];
                    switch (message.Type.ToLower())
                    {
                    case "visitor":
                    case "heed":
                    case "digg":
                        message.User          = new User();
                        message.User.Avatar   = "/Assets/Image/" + message.Type + ".png";
                        message.NewUsersCount = Convert.ToInt32((string)item["newUsersCount"]);
                        break;

                    case "message":
                    case "admin":
                        message.NewCount      = Convert.ToInt32((string)item["newCount"]);
                        message.TotalCount    = Convert.ToInt32((string)item["totalCount"]);
                        message.User          = new User();
                        message.User.ID       = Convert.ToString(item["user"]["id"]);
                        message.User.NickName = Convert.ToString(item["user"]["nickName"]);
                        message.User.Url      = Convert.ToString(item["user"]["url"]);
                        message.User.Sex      = ("人妖,他,她").Split(',')[(int)item["user"]["sex"]];
                        message.User.Avatar   = Convert.ToString(item["user"]["avatar"]);
                        message.User.IsOnline = ((string)item["user"]["isOnline"]).Equals("1") ? "在线" : "离线";
                        message.User.Age      = Convert.ToString(item["user"]["age"]) + " " + Love.Resources.AppGlobalStatic.AgeToAnimal((string)item["user"]["age"]);
                        break;
                    }

                    MessageListCollection.Add(message);
                }

                if (MessageListCollection.Count > 0)
                {
                    MessageListCollection.Add(new Message()
                    {
                        IsMoreButtonTrend = true
                    });
                }

                PageIndex_Message++;
                Dispatcher.BeginInvoke(() =>
                {
                    list_message.ItemsSource = MessageListCollection;
                });


                streamResponse.Close();
                streamReader.Close();
                response.Close();
            }
            catch (WebException e)
            {
            }

            Dispatcher.BeginInvoke(() =>
            {
                progressBar1.IsIndeterminate = false;
                ApplicationBar.Mode          = ApplicationBarMode.Default;
            });
        }
Ejemplo n.º 2
0
        private void GetMessageCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpWebRequest  webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                HttpWebResponse response;

                // End the get response operation
                response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
                response.Headers[HttpRequestHeader.ContentType] = "application/json; charset=UTF-8";

                Stream       streamResponse = response.GetResponseStream();
                StreamReader streamReader   = new StreamReader(streamResponse);
                var          Response       = streamReader.ReadToEnd();

                dynamic json = JsonConvert.DeserializeObject(Response);

                MessageListCollection = new ObservableCollection <Message>();

                foreach (var item in json["list"])
                {
                    var message = new Message();
                    message.ID          = Convert.ToString(item["id"]);
                    message.RichContent = (string)item["richContent"];
                    message.Source      = AppGlobalStatic.Source((string)item["source"]);

                    message.PrettyTime = AppGlobalStatic.DateStringFromNow(((string)item["prettyTime"]).Replace("今天", DateTime.Now.ToString("yyyy-MM-dd")));

                    message.User          = new User();
                    message.User.ID       = Convert.ToString(item["user"]["id"]);
                    message.User.NickName = Convert.ToString(item["user"]["nickName"]);
                    message.User.Sex      = ("人妖,他,她").Split(',')[(int)item["user"]["sex"]];

                    message.ToUser          = new User();
                    message.ToUser.Url      = Convert.ToString(item["toUser"]["url"]);
                    message.ToUser.NickName = Convert.ToString(item["toUser"]["nickName"]);
                    message.ToUser.Sex      = ("人妖,他,她").Split(',')[(int)item["toUser"]["sex"]];

                    message.RelatedData = item["relatedData"] as JObject;


                    message.Type = (string)item["type"];
                    if (message.Type.Equals("1"))
                    {
                        if (message.User.ID.Equals(App.MyLove["id"].ToString()))
                        {
                            message.RichContent = "我赞了" + message.ToUser.Sex + "发布的动态";
                        }
                        else
                        {
                            message.RichContent = message.User.Sex + " 赞了我发布的动态";
                        }
                    }
                    else
                    {
                        message.RichContent = AppGlobalStatic.ImgToEmotion(message.RichContent);
                    }
                    if (message.RelatedData["trend"] != null)
                    {
                        message.RichContent += "\n-    -    -    -    -    -    -    -   -\n" + AppGlobalStatic.ImgToEmotion(Convert.ToString(message.RelatedData["trend"]["richContent"]));
                    }

                    message.RichContent = AppGlobalStatic.ClearAhref(message.RichContent);

                    message.User.Url = Convert.ToString(item["user"]["url"]);
                    message.User.Age = Convert.ToString(item["user"]["age"]) + " " + Love.Resources.AppGlobalStatic.AgeToAnimal((string)item["user"]["age"]);

                    MessageListCollection.Add(message);
                }
                int pageCount = (int)json["page"]["pageCount"];
                int pageNo    = (int)json["page"]["pageNo"];

                if (MessageListCollection.Count > 0)
                {
                    if (pageCount != pageNo)
                    {
                        MessageListCollection.Add(new Message()
                        {
                            IsMoreButtonTrend = true
                        });
                    }
                }

                PageIndex_Message++;
                Dispatcher.BeginInvoke(() =>
                {
                    list_message.ItemsSource = MessageListCollection;
                });

                streamResponse.Close();
                streamReader.Close();
                response.Close();
            }
            catch (WebException e)
            {
            }

            Dispatcher.BeginInvoke(() =>
            {
                progressBar1.IsIndeterminate = false;
                ApplicationBar.Mode          = ApplicationBarMode.Default;
            });
        }