private void GetUserTimelineCallback(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); TrendListCollection = new ObservableCollection <Trend>(); foreach (var item in json["list"]) { var trend = new Trend(); trend.ID = Convert.ToString(item["id"]); //trend.Content = Convert.ToString(item["richContent"]); trend.Timestamp = AppGlobalStatic.DateStringFromNow(AppGlobalStatic.ConvertLocalFromTimestamp(long.Parse((string)item["timestamp"])).ToString()); trend.DiggCount = Convert.ToInt32((string)item["diggCount"]); trend.User = new User(); trend.User.ID = Convert.ToString(item["user"]["id"]); trend.User.NickName = Convert.ToString(item["user"]["nickName"]); trend.User.Url = Convert.ToString(item["user"]["url"]); trend.User.Sex = ("人妖,他,她").Split(',')[(int)item["user"]["sex"]]; trend.User.Avatar = Convert.ToString(item["user"]["avatar"]); trend.User.IsOnline = ((string)item["user"]["isOnline"]).Equals("1") ? "在线" : "离线"; trend.User.Age = Convert.ToString(item["user"]["age"]) + " " + Love.Resources.AppGlobalStatic.AgeToAnimal((string)item["user"]["age"]); trend.User.Height = Convert.ToString(item["user"]["height"]); trend.User.Education = AppGlobalStatic.Xueli[int.Parse((string)item["user"]["education"])]; if (item["isLoveNotes"] != null) { trend.IsLoveNotes = true; trend.Content = (string)item["letterContent"]; trend.LetterTemplateId = (string)item["letterTemplateId"]; } else if (item["isQaTrend"] != null) { trend.IsQaTrend = true; trend.Content = (string)item["content"]; trend.QA = new Question(); trend.QA.ID = (string)item["mediaInfo"]["question"]["id"]; trend.QA.Title = (string)item["mediaInfo"]["question"]["title"]; trend.QA.AnswerID = (int)item["mediaInfo"]["question"]["answerId"]; trend.QA.Options = new List <QuestionOption>(); int _index = 1; foreach (var ss in item["mediaInfo"]["question"]["options"]) { QuestionOption qo = new QuestionOption(); qo.Checked = (_index == trend.QA.AnswerID); qo.Text = (string)ss["text"]; trend.QA.Options.Add(qo); _index++; } } else { trend.IsPhotoTrend = true; trend.Content = (string)item["richContent"]; trend.Photolist = new List <Photo>(); foreach (var ss in item["mediaInfo"]["photoList"]) { trend.Photolist.Add(new Photo() { ID = (string)ss["id"], URL = (string)ss["url"] }); } } trend.Source = AppGlobalStatic.Source((string)item["source"], trend.Content); trend.Content = AppGlobalStatic.ClearAhref(trend.Content); TrendListCollection.Add(trend); } if (TrendListCollection.Count > 0) { pageToken = (string)json["page"]["pageToken"]; TrendListCollection.Add(new Trend() { IsMoreButtonTrend = true }); } Dispatcher.BeginInvoke(() => { list_trend.ItemsSource = TrendListCollection; //textblock1.Text = Response; }); streamResponse.Close(); streamReader.Close(); response.Close(); } catch (WebException e) { } Dispatcher.BeginInvoke(() => { progressBar1.IsIndeterminate = false; ApplicationBar.Mode = ApplicationBarMode.Default; }); }
private void GetHTMLCallback(IAsyncResult asynchronousResult) { HttpWebRequest webRequest = null; HttpWebResponse response = null; try { webRequest = (HttpWebRequest)asynchronousResult.AsyncState; response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult); //response.Headers[HttpRequestHeader.ContentType] = "application/json; charset=UTF-8"; Stream streamResponse = response.GetResponseStream(); StreamReader streamReader = new StreamReader(streamResponse); Response = streamReader.ReadToEnd(); MatchCollection mc = Regex.Matches(Response, "<span class=\"profile-label\">(.|\n)*?</span>(.|\n)*?</li>", RegexOptions.IgnoreCase); InfoListCollection = new ObservableCollection <Profile>(); foreach (Match mcc in mc) { string[] arr = Regex.Split(mcc.Value, "</span>"); var pro = new Profile(); if (arr.Length >= 1) { pro.Title = arr[0].Replace("<span class=\"profile-label\">", ""); } if (arr.Length >= 2) { pro.Value = AppGlobalStatic.ClearAhref(arr[1].Replace("\r\n", "").Replace("</li>", "")); } InfoListCollection.Add(pro); } Dispatcher.BeginInvoke(() => { list_info.ItemsSource = InfoListCollection; }); streamResponse.Close(); streamReader.Close(); response.Close(); } catch (WebException e) { } finally { if (webRequest != null) { webRequest.Abort(); } if (response != null) { response.Close(); } } Dispatcher.BeginInvoke(() => { progressBar1.IsIndeterminate = false; }); }
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; }); }