环境:
VS2012 更新 4
Windows Phone 8 SDK
一个全新的基于 WP OS 7.1 的 Windows Phone 项目
NuGet pack1:MS 异步
NuGet pack2:MS HttpClient
项目是做什么的
论坛 API 的单个测试:
在论坛中发布新主题并刷新 JSON 以检查“计数”是否累积。
UI 是什么意思
第一个文本 block :
响应 json 将显示在这里
3个按钮:
[刷新JSON]:使用API1刷新线程列表
[新帖]:使用API2在论坛发帖,标题和正文会显示在下方
[IE]: 使用API1在IE中打开json
线程信息:
您刚刚发布的帖子使用[新帖子]按钮
JSON是什么意思
只需要关注2个部分
计数:244 ---> 论坛中的线程总数
"57923"---> 第一个线程的标题(字段“data”是线程列表的数组)
什么是测试程序
1. 打开应用程序
2.点击【刷新JSON】获取列表
3.记住第一个线程的“计数”部分和“标题”
4.点击【新帖】发布新话题,话题标题会显示在下方
5.点击【刷新JSON】查看最新的话题列表
6. 预期:“计数”加1,“标题”部分应与下面相同
问题:
1.点击【新帖】后,无论点击多少次【刷新JSON】,计数都不会累加!
2. 但是在Chrome(Desktop)中打开url,你会发现JSON已经更新了!
3. 关闭应用程序并重新打开它,点击[刷新JSON],JSON 将按预期更新,但是当您发布新主题时,问题再次出现
为什么以及如何解决?
代码
private async void RefreshJSONButton_Click(object sender, RoutedEventArgs e)
{
this.JsonView.Text = string.Empty;
HttpClient client = new HttpClient();
string url =
"API1 Address Here";
string x = await client.GetStringAsync(url);
this.JsonView.Text = x;
client.Dispose();
}
private async void NewPostButton_Click_1(object sender, RoutedEventArgs e)
{
Random rnd = new Random();
this.num = rnd.Next(1, 99999);
// generate the title and content with random number
string threadTitle = this.num.ToString();
string threadContent = "111" + num.ToString();
// generate the url
string url =
"API2 Address Here";
url = url + "&title=" + threadTitle + "&content=" + threadContent;
// use HttpClient to send the new thread
HttpClient client = new HttpClient();
string x = await client.GetStringAsync(url);
client.Dispose();
// display the thread informartion for further check
this.titleView.Text = threadTitle;
this.contentView.Text = threadContent;
}
请您参考如下方法:
看起来,您在每次刷新点击时都点击了相同的 URL。有时 Windows Phone 将请求和响应存储在缓存中。
如果可能,您可以在请求中再添加一个参数,每次点击时只需更改此参数的值即可。您可以使用 GUID 或 Unix 时间戳。
你可以这样尝试:
private async void RefreshJSONButton_Click(object sender, RoutedEventArgs e)
{
this.JsonView.Text = string.Empty;
HttpClient client = new HttpClient();
int unixTimestamp = (int)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
string url =
"http://API1 Address Here/Your queries&temp=unixTimestamp";
string x = await client.GetStringAsync(url);
this.JsonView.Text = x;
client.Dispose();
}
