public Page1() { this.InitializeComponent(); hotpost_list.ItemsSource = PostTitle; httpClient = new HttpClient(); cts = new CancellationTokenSource(); HttpRequestAsync(async () => { string resourceAddress = server; string responseBody; HttpResponseMessage response = await httpClient.GetAsync(new Uri(resourceAddress)).AsTask(cts.Token); responseBody = await response.Content.ReadAsStringAsync().AsTask(cts.Token); var serializer = new DataContractJsonSerializer(typeof(PostInfo)); //将json字符串解析成class var mStream = new MemoryStream(Encoding.Unicode.GetBytes(responseBody)); hotpost = (PostInfo)serializer.ReadObject(mStream); init_list(hotpost); return responseBody; }); }
public void init_list(PostInfo posts) { int i; for(i=0;i<hotpost.hotSum;i++) PostTitle.Add(new Posts { title=hotpost.posts[i].title}); }